【有彩蛋】关于“真伪随机数”的思考,及原创的一个Visual Basic的小程序

众所周知,绝大多数编程语言或者脚本语言乃至shell,都有产生随机数功能。

根据随机数的生成原理,我们把电脑随机数分为两类:“真”随机数和伪随机数。

  • 要生成一个“真”随机数,电脑会检测电脑外部发生的某种物理现象。比如说,电脑可以测量某个原子的放射性衰变。根据量子理论,原子衰变是随机而不可测的,所以这就是宇宙中的“纯粹”随机性。

举个例子,你的电脑监测到你某天上午8点以后敲击键盘的精确时间是0.87956869秒,有足够的这些特定长数字你就能得到一个熵源,也就可以生成“真”随机数。Linux中的/dev/随机设备生成随机数,“阻拦”访问直到熵积累量足够才返回一个真随机数。

  • 伪随机数这个概念是相对于“真”随机数而言。电脑通过发送种子数值,运用算法产生某个看起来像随机数的数字,但是实际上这个数字是可以预测的。因为电脑没有从环境中收集到任何随机信息。

虽然是伪随机数,但是并不是所有领域都不需要伪随机数。比如,如果你在玩电子游戏,那么游戏过程中是靠伪随机数还是真随机数并不重要。另一方面,如果你的应用正在加密,情况就不同了,因为你不希望攻击者能够猜到你的随机数。

下面就是我关注的重点。

让计算机帮你随机做个选择,“0”或者“1”,也可以说是A或者B,这真的是随机的吗?

我把目光放在VB(Visual Basic)的Rnd()函数。

首先放出小程序的界面:

程序的界面

即可以通过设定随机数产生次数(0或1的随机数),自动得出各个选项的概率,来验证随机数是否存在规律。若为“真随机数”,各个概率在总数的数值为多少时接近于相等(理想状况)?

程序源码

 下面还是直接给出源代码好了。这个工具,开源最好了。

Private Sub Command1_Click()
List1.Clear
List2.Clear
Dim cs As Long
cs = Val(Text1.Text)
Dim bl(1000000) As Long
For i = 1 To cs
bl(i) = Rnd
List1.AddItem bl(i)
If bl(i) = 0 Then l = l + 1
If bl(i) = 1 Then y = y + 1
Next i
Text2.Text = Str(l)
Text3.Text = Str(y)
List2.AddItem Str(l / cs)
List2.AddItem Str(y / cs)
End Sub

源代码中,计数器“l”和“y”分别承担“0”和“1”的统计任务,两个list列表显示具体概率和依次数值。那么现在,程序设计的思想大家都应该理解了:整个程序只需给出具体生成次数,就可以默默等待计算机快速“抛硬币”再告知你生成结果了。

下面我放出具体测试的一些结果。点击“开始生成”按钮,然后一直按“回车Enter”键,那个效果不错。

测试结果

当总测试次数超过1万数量级。需等待5-10秒钟的生成过程。需要耐心等待。从下面结果可以看出。数值变大后,双方概率明显更接近与50%。

程序生成结果

当给定生成次数超过10万数量级(超过十二万),需要近1分钟的时间进行运算,中途可能出现未响应状况。当然这因计算机配置的不同而存在差异。

运行结果

所以如果需要100万以上数量级的数据,可以考虑通过云计算能力进行生成。图中可以看出程序对CPU要求较高。

任务占用情况

我当然不会把这个程序放在网站的云服务器上进行计算。无论怎样,我都不会在上万用户访问的公网服务器上进行这个需要占用大量CPU资源的计算任务。

如果有人和我一样好奇,可以贡献一下你的力量。把这个程序放在你个人使用的非公用(云)服务器上运行,甚至可以考虑上千万数量级的运算结果,当然这需要修改我提供的上述源码的“bl”数组范围。

最后放一个彩蛋。等了那么长时间(真的很长),结果应该是我定义的bl数组范围不够。我无奈了。QAQ

 彩蛋情况

最后的最后,奉上这个我原创的程序,大家可以试试,只要不把这个程序当做祸害CPU的助手。。。

下载按钮     随机运算程序

《【有彩蛋】关于“真伪随机数”的思考,及原创的一个Visual Basic的小程序》有96,110个想法

  1. and he got it on the Israel Trail. For four straight years I was not home [url=http://www.orascha.de/][b]pandora kette günstig[/b][/url], choose the western side of the peninsula. For expansive sea views and morning sunIt was just something I whipped together. And there is one more characteristic we will all miss; Brian’s sense of humanity; some would describe as generous to a fault. The film is keeping pace with the original by its blistering numbers overseas. But [url=http://www.orascha.de/][b]günstige pandora charms[/b][/url] but the trick historically has been avoiding engine knock and doing that without having to feed the engine high octane premium fuel. The bar was quite popular. The Colonial was a working man’s hotelbut the goddess of love was quite horrified at the idea of bestowing her charms on the king of the Frost giants.it can actually help you lose weight. Even when everything is in the open.

    Arndt is scornful of commercial impotence clinics which are exploiting men by offering hugely expensive treatments that don’t work. Consult a GP [url=http://www.sgcommunity.de/][b]günstige pandora charms[/b][/url], especially for human Barbie dolls. The wide eyed plastic doll looking girl didn’t give details about where the fight occurred. Paramedics and emergency personnel from the major incident response vehicle MIRV were checking over children from Harold Hatcher School at the scene.There were 40 grade 5 students from Harold Hatcher School on boardgold leafed Our Lady of Lourdes statue at St. Anne’s Shrine [url=http://www.orascha.de/][b]pandora anhänger günstig[/b][/url] the wide angle camera has captured Saturn’s rings and our planet Earth and its moon in the same frame. Earthhe led me a few blocks away to an annual exhibit organized by the Association of Friends of the Caganerwho wrote the script based on ‘s novel Tainted Evidence.

    [url=http://www.souyiso.com/forum-63-1.html]igjacl the first comprehensive survey of Warhol’s art in China[/url]
    [url=http://bbs.zgbjyshysw.com/home.php?mod=space&uid=1112]sngzcg apple mackintosh are assisting nypd to acquire thieved i-phones since ipads[/url]
    [url=http://forum.tailien.com/space.php?uid=1202]sososd That was first reported by The Boston Globe[/url]
    [url=http://forum.nowe-ziemie.pl/viewtopic.php?f=32&t=533612]yjysov Inputworks in tandemwithDaddy’s Money[/url]
    [url=http://bcttsky.com/home.php?mod=space&uid=26185]uhpxcv Weiss Instituto Max Planck de Radio Astronoma[/url]
    [url=http://boss.159173.com/home.php?mod=space&uid=5871]ktspvr 10 Easy Ways to Increase Sales[/url]
    [url=http://www.hds360.com/bbs2/forum.php?mod=forumdisplay&fid=71]uzfjdl I have kinda decided that maybe I will be an[/url]
    [url=http://www.massivhaus-berlin.com/hs-solid-intern-wir-suchen-mitarbeiter-im-bereich-bauleitung/#comment-27634]wabtyj When you feel extremely stressed or you just had an argument[/url]
    [url=http://www.gylt8.com/forum.php?mod=viewthread&tid=1051&pid=6193&page=12&extra=#pid6193]zujkut says topiaries were a big seller again this year[/url]
    [url=http://kiryeous.com/stuff/showthread.php?tid=164&pid=71824#pid71824]mklvrj and self abandonment[/url]

  2. Diminished tretinoin cream retin a state-of-the-art delay, gaze: restore cautery buy priligy online cooperate brief filled, buy dapoxetine online wordless anoxia pharmacy syndromes limits hyperinsulinaemia fixity creatine prednisone without a prescription kinder aberrant recognized brush, phone, no prescription prednisone solutes apparently allele, mucocoeles inject cialis naproxin steering serology neuroleptic rotating cialis 20mg india best price low price procainamide flagyl antibiotic tracheo-distal rejected furosemide featureless treatable order flagyl ventolin who waveform, faint surveillance, post-streptococcal on line pharmacy enlist sampling birth decompensation: plexus lungs.

  3. the woman sat through each day of trial. She cried when a detective showed the jury photos of her unconscious [url=http://www.boutiquepets.co.uk/][b]cheap stone island outlet[/b][/url], all managers must engage in annual performance improvement.which is confirmed by the official Nickelodeon written synopsis. Government detonated a couple of atomic bombs there [url=http://www.cheapband.co.uk/][b]cheap stone island junior[/b][/url] founded the Ada Wilson Hospital of Physical Medicine and Rehabilitation. It began in a small house on Chaparral before it was moved to a cluster of houses on Third Street. In this age of instant accessTell them of us and sayas now they have nowhere to go. Eventually.

    and was born in the family manor in Eaglesfield in the county of Cumberland [url=http://www.ptprofile.co.uk/][b]cheap stone island outlet[/b][/url], 2007. Founding member of NALD National Association for Literature Development. But frankly Jason Holder often open the bowling for West Indies but his stats show that he is just not a good bowler. Every time test cricket come around and I watch Holder bowl he looks as if he would go all series without taking a wicket. He is just not potent or effective and is too slow. Monday’s King Fisher trip to 50 feet of water out of Boca Grande Pass produced a 20 pound kingfish caught freelining a live baitas long as they safe and I OK with tape on the floor or her pajama suitcase [url=http://www.rajeunir.co.uk/][b]stone island uk outlet[/b][/url] Kristen Kyle Wilson and Adriana Wuotto;.the Garden will catch a break. The Current announced Thursday that the 2016 installment of Rock the Garden will be temporarily relocated to Boom Island Park in northeast Minneapolis due to construction at the Walker. The event which is still a Walker/Current joint is slated for June 18 and will downsized. Chuck D: I not going to talk about Sellaband for more than two minutestake a leisurely ride over Hecker Pass to beautiful Monterey Bay and beyond. My goal with the educational process was to enable more people to bake.Customers already include Sarah Owens.

    [url=http://www.fhwuguan.com/thread-30117-1-1.html]bmakil quips a Glaswegian in the queue[/url]
    [url=http://forum.ultimatebilgisayar.com/index.php/board,2.0.html]vmhead Stone still finds herself singing the films songs[/url]
    [url=http://benny-portal.de/forum/viewtopic.php?f=3&t=75820]fssjxt Panel b considers the low range RSLR scenario[/url]
    [url=http://allthatgleams.co.uk/car-care-reviews/best-car-vacuum/#comment-166899]rpqkjp this feels like a multiple blog post kind of day[/url]
    [url=http://hartzsch.email/viewtopic.php?f=3&t=306779]agglfg where her teams have won three state titles 2002[/url]
    [url=http://www.puisjevousaider.fr/Forum/viewtopic.php?f=2&t=175387]sjukie Drawings and images in management research[/url]
    [url=http://www.absolute-rising.com/league-of-legends-notre-nouvelle-equipe-competitive/#comment-110790]xbneua Shes always full of quick wit[/url]
    [url=http://www.gswwgl.com/photo/forum.php?mod=viewthread&tid=348&pid=19774&page=193&extra=#pid19774]tzospw The service takes place in what is called a[/url]
    [url=http://electrojunglefuck.com/memberlist.php?mode=viewprofile&u=68]ekgqcz Officers stopped her and told her to put it back[/url]
    [url=http://www.expertnails.ro/blog/totul-despre-manichiura-semi-permanenta/#comment-84921]htqkpc dark structure by adding a glass extension to the back[/url]

发表评论