我以前正在研究生成一個隨機名稱列表,模仿他美國國家人口基數;所需的數千個名字模仿了美國人口中的重複使用。爲此,我需要一個名字列表以及Sur名稱和它們的使用頻率。因此,我從社會保障管理局獲取種子數據,以獲得1000個First和Sur名稱,以及它們的使用頻率。
Think of three columns. The first column is a list of names, the second their frequency seen in the population bases, and the third is the rolling totals of their frequency added row to row:
Marry, 57, 10
John, 40, 60
Lloyd, 2, 62
Zac, 1, 61
Read a seed file of any size into an area, along with the weight value for each name (or number). This script assigns the rolling total weight values, then generates a random number between 1 and the sum of all weights. Checks that random number against the rolling sum of weights to locate the number associated with that weight.
Looking at the above example you have a 57% chance to generate Marry, and a 1% chance to generate Zac.
The below script is a more robust example of the general idea. It will generate random numbers between 1 – 25, based on the probability of their weights. The portion of the script that generates the random numbers between 1 – 25 loops 128 times to give you 128 numbers based on their probability of being selected.
I basically used something like this to generate thousands of random names to mimic a population bases that mirrored the USA.
Dim iVar001, iVar002, iVar003
Dim iMin, iMax, iRand, iRandN
Dim iRow, iCol
Dim aName
Dim aList()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' iMin is the lower range of random numbers, iMax is the upper
' range of random numbers being generated
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
iMin = 1
iMax = 25
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Create a dinamic sized 2D area used to generate a random number
' based on weighted Probability. Array values array(0, 0) threw
' array(x, 0), where 「x」 is the upper range value of the number
' to be generated, contains the range of random numbers you will
' be generating. Array values array(0, 1) to array(x, 1) contains
' the weight value for each number. Array values array(0,2) to
' array(x,2) contains the rolling sun of the weight values.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ReDim aList(iMax - 1, 2)
iVar001 = 0
Do While iVar001 <= (iMax - 1)
Randomize
aList(iVar001, 0) = iVar001 + 1
aList(iVar001, 1) = Int(((20 * iMax) - iMin + 1)* Rnd + iMin)
If iVar001 = 0 Then
aList(iVar001, 2) = aList(iVar001, 1)
Else
aList(iVar001, 2) = aList(iVar001, 1) + aList(iVar001 - 1, 2)
End If
aName = aName & aList(iVar001, 0) & " - " & aList(iVar001, 1) & _
" - " & aList(iVar001, 2) & vbCrLf
iVar001 = iVar001 + 1
Loop
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Generate a message box containing the array values.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
MsgBox ("List of numbers," & vbCrLf& "weight values and" & _
vbCrLf & "totals of weights." & vbCrLf & vbCrLf & aName)
aName = ""
iCount000 = 1
iCount001 = 1
iCol001 = 0
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Generate a randon number between 1 and the total of weight
' values. Then step threw the array values until the the random
' value is nolonger less than or equal to the running wieght value,
' and record the number associated with that running weight value.
' The count of random =numbers generates is controled by the
' following Do While iCount000 <= 198.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''
Do While iCount000 <= 198
Randomize
iRand = Int((aList(UBound(aList,1),UBound(aList,2)) - iMin + 1)* Rnd + iMin)
iVar001 = 0
Do While iRand >= aList(iVar001, 2)
iVar001 = iVar001 + 1
iRandN = aList(iVar001, 0)
Loop
iCount000 = iCount000 + 1
If iCount001 = "19" Then
aName = vbCrLf & aName
iCount001 = 0
Else
iCount001 = iCount001 + 1
End If
aName = iRandN & ", " & aName
Loop
MsgBox "List of random numbers in range " & iMin & " to " & iMax & "," & vbCrLf & _
"based on weighted probability." & vbCrLf & vbCrLf & aName
真是個好主意,從來沒有像那樣看過它。但我不完全確定什麼會工作,因爲我想要它。只要確定隨機值需要在1到12之間,我需要選擇3個數字。 –
聽起來很棒,也許可以工作。你有一段代碼示例嗎?我有點不確定該怎麼做? –
增加了一些代碼 –