I think this will do it.
Put the number of species A in A1, species B in B1, species C in C1.
From your figures that would be A1=13, B1=53, C1=20
Leave D1 blank.
Put the total sample size you want in E1. E1 = 50
Run this macro and 1000 random choices will be made and counted.
After the macro is run, G1:I100 will show the totals for each species per choice.
Sub randomBugs()
Dim allBugs() As String, bugCount As Long
Dim sampleSize As Long, numSelections As Long
Dim aCount As Long, bCount As Long, cCount As Long
Dim outRRay() As Double
Dim writeRange As Range
Dim i As Long, temp As String, rowNum As Long
Dim pointer As Long, randIndex As Long
Rem adjust the sheet name as needed
Set writeRange = ThisWorkbook.Sheets("Sheet1").Range("g1")
aCount = writeRange.Parent.Range("a1")
bCount = writeRange.Parent.Range("b1")
cCount = writeRange.Parent.Range("c1")
bugCount = aCount + bCount + cCount
sampleSize = writeRange.Parent.Range("E1")
numSelections = 1000
Rem fill allBugs {a,a,..,b,b,...,c,c...}
ReDim allBugs(1 To bugCount)
pointer = 1
For i = 1 To aCount
allBugs(pointer) = "a"
pointer = pointer + 1
Next i
For i = 1 To bCount
allBugs(pointer) = "b"
pointer = pointer + 1
Next i
For i = 1 To cCount
allBugs(pointer) = "c"
pointer = pointer + 1
Next i
ReDim outRRay(1 To numSelections, 1 To 3)
Rem do 1000 times
For rowNum = 1 To numSelections
Rem choose some bugs
For i = 1 To sampleSize
Randomize
randIndex = 1 + Int(Rnd() * bugCount)
temp = allBugs(i)
allBugs(i) = allBugs(randIndex)
allBugs(randIndex) = temp
Next i
Rem count them
For i = 1 To sampleSize
Select Case allBugs(i)
Case "a"
outRRay(rowNum, 1) = outRRay(rowNum, 1) + 1
Case "b"
outRRay(rowNum, 2) = outRRay(rowNum, 2) + 1
Case "c"
outRRay(rowNum, 3) = outRRay(rowNum, 3) + 1
End Select
Next i
Next rowNum
writeRange.Resize(UBound(outRRay, 1), UBound(outRRay, 2)).Value = outRRay
End Sub
I hope this helps.
Bookmarks