Lua,随机是随机的吗?

Use*_*r.1 3 random lua

我应该验证我们实现Lua的随机数生成器的能力.

这就是我提出来的......

 for i = 1,10000 do                         -- I hope that's ten thousand

 X = math.random(0,9)

 if     X == 0 then This_Many_0 = This_Many_0 + 1
 elseif X == 1 then This_Many_1 = This_Many_1 + 1
 elseif X == 2 then This_Many_2 = This_Many_2 + 1
 elseif X == 3 then This_Many_3 = This_Many_3 + 1
 elseif X == 4 then This_Many_4 = This_Many_4 + 1
 elseif X == 5 then This_Many_5 = This_Many_5 + 1
 elseif X == 6 then This_Many_6 = This_Many_6 + 1
 elseif X == 7 then This_Many_7 = This_Many_7 + 1
 elseif X == 8 then This_Many_8 = This_Many_8 + 1
 elseif X == 9 then This_Many_9 = This_Many_9 + 1

 else               Bogus_Alert = True      -- Better not happen

 end                                        -- End the big if/elseif block

 The_Interim_Sum = The_Interim_Sum + X      -- Keep a running sum

 end                                        -- This is the i loop
Run Code Online (Sandbox Code Playgroud)

然后我打印出结果,这里是......

 Running
 Number of times we got 0  ...  1019
 Number of times we got 1  ...  979
 Number of times we got 2  ...  954
 Number of times we got 3  ...  1006
 Number of times we got 4  ...  995
 Number of times we got 5  ...  999
 Number of times we got 6  ...  989
 Number of times we got 7  ...  1000
 Number of times we got 8  ...  1042
 Number of times we got 9  ...  1017
 The sum of the ten thousand random numbers was 45303
 The average of those numbers was 4.5303
 End
Run Code Online (Sandbox Code Playgroud)

这些都符合我的期望; 即,平均值非常接近4.5,每个个体数的分布接近千.

更重要的是,对于像这样的数字,我说的确是,确实,生成器在制作一万个真正随机的随机数方面做得非常好.

这里有一个混乱的地方:我的老板说,如果机器真正做好了工作,真正给我们随机数字,那么每个数字的分布应该是均匀的; 也就是说,在一万次迭代中,我应该得到每个数字的一​​千个.

我尝试math.randomseed(os.time())在循环之前添加,这是X = math.random(0,9)基于StackOverflow上的其他一些对话使用的.结果进一步消失,而不是更接近.

我拿出了这math.randomseed(os.time())条线,再次进行了测试,得到了平均4.5007(这是我见过的最好的).

那么,我发现了什么?还有什么?没有 ?随机数生成器实际上是*伪*随机数生成器?每个人都已经知道了.

我是否证明了这个伪随机数发生器几乎和我们预期的一样随机?

我有理由担心使用这些值吗?我打算在常规赛中投篮,并故意给他糟糕的数据.全范围对于测试每个案例都太过分了.我猜测正确分布,16K的蠢事会让我对芯片正确处理(故意坏的)值的信心.

(请注意读者,我在多处理器系统上进行V&V操作,而我的Lua脚本伪造了系统中我们尚未拥有的部分的行为.)

在这种情况下,有没有一种方法可以让Lua生成每个数字中的一千个math.random()函数?

如果是这样,那真的是随机的吗?

Yu *_*Hao 5

首先,你需要math.randomseed()在使用之前运行一次math.random(),并且最好扔掉前几个随机数,因为前几个在某些实现中不是随机的,请参阅Lua math.random不适用于细节.

math.randomseed(os.time())
math.random(); math.random(); math.random()
Run Code Online (Sandbox Code Playgroud)

其次,即使你翻动一千倍完美平衡的硬币,你通常不会得到精确的头500倍和尾部的500倍.只是获得头部的概率大约是500.如果实验已经进行了很多次,你会发现头部的平均值是500,但是每次都不是500.

第三,是的,math.random()是一个psudo随机数生成器.实际上,您无法仅使用计算机生成实际随机数.Lua math.random()rand()内部使用C库函数.而且C rand()函数确实不是一个好的随机数生成器.如果随机性很重要,请使用一些更好的方法来实现,例如,C++随机引擎,或者dev/random在Linux上等.