2015-11-19 93 views
3

我試圖將概率添加到我的隨機數生成器中。如何將概率添加到隨機數生成器中

這是隨機數發生器代碼。

'Generate 3 random numbers' 
Dim rn As New Random 
Dim result1, result2, result3 As Integer 
result1 = rn.Next(1, 12) 
result2 = rn.Next(1, 12) 
result3 = rn.Next(1, 12) 

所以這段代碼會生成3個隨機數並將它們保存到不同的變量中。 我想要發生以下事情。

1 has a 20% chance of being selected 
2 has a 28% chance of being selected 

值需要在1和12之間,並且我需要選擇3號沿該線 東西。

我發現this but it seems to be a little off topic?

難道這可用於我想要什麼?

回答

4

製作一個包含100個項目的數組。將數值1分爲數組中的20個點,數值2分爲數組中的28個點,依此類推。然後選擇一個隨機數組索引。根據您的值和概率,您可以簡化或調整陣列的大小。

或者,您可以只存儲邊界對({1,20},{2,48},...),獲取一個小於最高邊界值的隨機數,然後找到最小對的數字你的邊界值大於或等於隨機結果。這裏有一個例子:

Private rnd As New Random() 
Public Function GetValue() As Integer 
    'max value is 100 
    Dim boundaries = { 
     {20, 48, 56, 60, 69, 74, 77, 82, 84, 88, 92, 100}, 
     { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} 
    } 

    Dim r As Integer = rnd.Next(1, 101) 'upper range is exclusive 
    For i As Integer = 0 To boundaries.GetUpperBound(1) 
     If boundaries(0, i) >= r Then Return boundaries(1, i) 
    Next 
    Throw New Exception() 'code should not be able to get here 
End Function 

Dim result1 As Integer = GetValue() 
Dim result2 As Integer = GetValue() 
Dim result3 As Integer = GetValue() 
+0

真是個好主意,從來沒有像那樣看過它。但我不完全確定什麼會工作,因爲我想要它。只要確定隨機值需要在1到12之間,我需要選擇3個數字。 –

+0

聽起來很棒,也許可以工作。你有一段代碼示例嗎?我有點不確定該怎麼做? –

+0

增加了一些代碼 –

-2

我以前正在研究生成一個隨機名稱列表,模仿他美國國家人口基數;所需的數千個名字模仿了美國人口中的重複使用。爲此,我需要一個名字列表以及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