html随机数怎么控制概率,如何将概率添加到随机数生成器中

我以前正在研究生成一个随机名称列表,模仿他美国国家人口基数;所需的数千个名字模仿了美国人口中的重复使用。为此,我需要一个名字列表以及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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值