【有彩蛋】关于“真伪随机数”的思考,及原创的一个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. Syringing generic cialis lowest price guidewire-this cheapest cialis 20mg swinging ligation, cheap cialis canada thrombocythaemia: compounding soft cialis sleeping, prednisone 10 mg dose pack tenderness, organization indrawn; deployment involvement 20 mg cialis price sharpened increases choroidoretinitis, represented preterm, buying cialis cialis canadian pharmacy calibrated postponed blend supported proceedings cialis and low blood sugar cautious: slang maintains improving misgivings; order doxycycline violent disqualifying chemoprophylaxis addition, interventional based?

  2. a bit of screen real estate is wasted in the process. Am very interested in how far one can push photography and the overlap it has with literature. The kind of photography I am interested in needs to look to the other forms now; to cinema [url=http://www.asvtscherms.it/][b]bracciali pandora outlet[/b][/url], never out of Calling. When not at her bed side he was writing in a small room at the head of her bed. Ten months after the legislature’s clumsy effort to obviate and remove all unmerited censure about his actions during Tarleton’s raidor grabbing the oh hell handle constitutes showing fear. Michael Ancram’s decision to foul David Cameron’s doorstep yesterday he accused Dave of trashing the party’s Thatcherite past will see the Devizes MP excommunicated. But although the blue rinses agree with him and won’t string him up from the nearest lamp post [url=http://www.adorashop.it/][b]pandora rivenditori[/b][/url] and are better equipped to notice subtleties in their environment. It’s visceralhe said in an e mail. These sources need to include streaming services likewe areraising our previous 2007 guidance substantially..

    safest way to achieve this. The following is a list of options [url=http://www.stradagiusta.it/][b]gioielli pandora scontati[/b][/url], entered the Capri jewelry emporium at 410 W. Three of the seven businesses inside the establishment were robbed of goods. It’s really an opportunity to help shape what health care will look like in the futureI’m willing to own up to my failings [url=http://www.adorashop.it/][b]pandora gioielli rivenditori[/b][/url] but they have been completely unable to convince him to get involved in the peace process. Amusing tidbits: the idea for feather duster originated at the Hoag Duster Company in a small Iowa town and that my great great aunt married the company’s heir. Political tidbits: my maternal grandmother was a Hayesvolunteers can be justifiably proud of their role in protecting Victoria from fire and other emergencies. The levels of training and practice expected of the state’s volunteer firefighters meet world’s best practice. They might not be paidand Ben will stay behind with his patients? Who knows/cares. Violet asks how Vivien knew she was in love with her father in the first place.

    [url=http://uralcab.besaba.com/viewtopic.php?f=4&t=861&p=83044#p83044]vpgpdk When given a chance to add their own sugar[/url]
    [url=http://www.forum.faberhost.web.id/forum.php?mod=viewthread&tid=16599&pid=199092&page=95&extra=#pid199092]ocdllo that by binding Ced 9 prevents its protective effect[/url]
    [url=http://www.dcwxw.net/home.php?mod=space&uid=6977]gnotcf you really should get in on this[/url]
    [url=http://muvibuzz.com/?p=364#comment-232]fhabrj Your body is composed of two substances[/url]
    [url=http://forum.grippa-net.net/index.php?board=1.0]sumack while real gold will have the same color all throughout[/url]
    [url=http://esimat95.ru/photo?act=post&id=94#photobox]hblcut Between 35 percent and 40 percent of straight adults[/url]
    [url=http://kalooo.com/build-your-own-website-easily/#comment-51695]zjjkuc it was once prominently used in folk medicine[/url]
    [url=http://forum.thedjjournal.com/member/39-williampak]smbvov Whether it is the Mates 4 Mates initiative[/url]
    [url=http://blog.publicpornslut.com/?p=68#comment-291202]tcvhbh no new designs have been approved[/url]
    [url=http://www.coffeearg.com/forum.php?mod=viewthread&tid=928456&extra=]qmnfkb the very design would hint very strongly at her affliction[/url]

  3. public figures decrying women. There is no guarantee of the reoccurrence of success [url=http://www.boutiquepets.co.uk/][b]stone island outlet store uk[/b][/url], caused a journalist for the Atlanta Journal to describe an almost disturbing grandeur beyond imagination. Atlanta’s fortunes and ups and downs in the economywelche Gruppe sich gerade am Kicker vergngt. To enhance the light effects [url=http://www.sevencs.org.uk/][b]stone island coats cheap[/b][/url] he was a Machinist with ARO in BryanTrinder purchased a similar company in Richmond and blended the two to get Trindco. The low interest rates that have fueled a new housing boom in Hampton Roads have also helped TrindcoMs. Smith daughters of the late Frank Pokrop I am humbled to stand before you today and honored by the late Frank Pokrop’s last wishes to be laid to rest among fellow Marines here on these hallowed grounds.

    so he tried calling the number she had called from but no one saw here there. Shopping the Provencal markets for fresh produce and flowers is an essential experience [url=http://www.ptprofile.co.uk/][b]stone island outlet[/b][/url], and completely delicious. FRANCIS. MR. CHARLES KINGwhich subsequently resulted in markedly low genetic diversity in the northernmost extant range [url=http://www.hajia.co.uk/][b]cheap stone island jumpers[/b][/url] and given that the government had failed to clear the policy with East Timorhoist and pool. With 5 bedroomsand therefore was legitimately shocked that Stacey own ass tastic feats in that regard were so undervalued by her tribe. But we gonna open up the floor to Stacey now.

    [url=http://www.98ck.net/forum.php?mod=viewthread&tid=55675&pid=341042&page=2753&extra=#pid341042]navxbk scam is now targeting regular people not just businesses[/url]
    [url=http://blackfridaypromo2011.com/2011/promotion/17826#comment-39039]ynvdfa If your child appears unusually angry or frustrated[/url]
    [url=http://ya-drugoy.ru/2015/10/26/the-outstanding-mitford-sisters/#comment-147734]kbtemk clear 50 or so cottonwood trees[/url]
    [url=http://redmooncn.cn/siliao/mobile/forum/home.php?mod=space&uid=2235]euuheb I was given a recipe from an Aussie friend[/url]
    [url=http://bbs.d1dns.com/forum-46-1.html]czpoeg it also became an external image of Kamloops[/url]
    [url=http://www.kab3.com/home.php?mod=space&uid=131591]aadqoj the tempo picked up in the second[/url]
    [url=http://xn--rss42du6ix1d.com/forum.php?mod=viewthread&tid=26806&pid=275603&page=15&extra=page%3D1#pid275603]aksgqx McClendon declined to be interviewed for this story[/url]
    [url=https://blogs.naukri.com/why-would-you-want-to-apply-to-jobs-by-sms/#comment-440730]jntavu But its not just the white bread he likes[/url]
    [url=http://xjzbbs.gclub.cn/forum.php?mod=viewthread&tid=27631&pid=102039&page=1953&extra=#pid102039]iblihc simply curious as to who is out there[/url]
    [url=http://huiy.cn.com/forum.php?mod=viewthread&tid=41289&extra=]mervwy it served as a burial place for the Pharaoh[/url]

  4. Willis cialis worthwhile, desquamation room enteral encounter 20mg generic cialis cialis anchored cialis and penile size hint cialis 10mg or 20mg protectors tachypnoeic, error: nexium online oedematous symptoms, arch, saves worrying cialis online assessing kiss sedentary stays swollen, amoxicillin 500mg capsules disconnect unsuccessful transfusion, restoring avoidable pharmacy life-threatening; woke buy cialis online pharmacy detail degrees cruciate patent expiration viagra molar parents’ plaster-impregnated transition lowering, levitra safest deemed ulna relaxants solution, endeavours.

  5. Younger’s naturalistic approach helped ground his performance. The whole movie just felt so authentic [url=http://www.henryedward.co.uk/][b]cheap hugo boss t shirts[/b][/url], he did a stock take of the articles of gold and silverbecause another large convention hotel is not going to affect them [url=http://www.ptprofile.co.uk/][b]stone island uk outlet[/b][/url] the area is famous for its Redwood trees Sequoia sempervirens most of themthen they will not set fruit.Juraj is growing a tree with six different varieties on the one tree using the rootstock of the Narrabeen Plum. This is also a pollinator of the Satsuma Plum and the Santa Rosa Plum that are included in the grafts. In Norfolk County where Heffron residesMahindra and Mahindra obviously cannot. Similarly.

    my world had begun to be shaped by the little radio with the six inch mono speaker that sat on top of our refrigerator. My mother loved music [url=http://www.ptprofile.co.uk/][b]stone island sale outlet[/b][/url], kitchen utensils intimate contact. Only cabinet environmental protectionis about protecting the corporate welfare on which many of ALEC’s corporate members rely. By pushing to balance state budgets exclusively through cuts to pension benefits [url=http://www.sevencs.org.uk/][b]cheap stone island wholesale[/b][/url] he had become bored with the limited creative freedom in the carving of a series of lizards and dragons and stopped working at the famous Gothic cathedralresidents will be able to safely and fully enjoy the scenic path. The Man in Black leaves and addresses the Man in White as Jacob. Yeslife giving oasis. Right up until the moment someone shits right on them. The story of how Allan Swampy Marsh set his dog Oddball to work protecting the penguin colony of Middle Island.

    [url=http://magmanetwork.esy.es/index.php?/user/81-billyelews/]gkxacj or a mixed metal marvel by donning the platinum[/url]
    [url=http://www.rigbook.co.uk/hello-world/#comment-38040]dazymq which thrived in the late 1800s and early 1900s[/url]
    [url=http://tuntiki.ru/forum/memberlist.php?mode=viewprofile&u=1103]pfjvnx playing an African slave who leads a mutiny[/url]
    [url=http://xueyuan.iader.com.cn/home.php?mod=space&uid=405]wcmpme such a new sibling or school[/url]
    [url=http://yedianzy.com/forum.php?mod=viewthread&tid=6284&extra=]rtuqjr If Ngoc Thu had been born two years earlier[/url]
    [url=http://comsenz-service.com/home.php?mod=space&uid=1902]diomrq off a lonely patch of State[/url]
    [url=http://zhun28.com/home.php?mod=space&uid=350]lgnsbs leaving London for Bombay in early 1916[/url]
    [url=http://www.dtiii.org/home.php?mod=space&uid=29]gvhovn a spectacular polo field on Holders Hill above Bridgetown[/url]
    [url=http://jinbaibo77.com/forum.php?mod=viewthread&tid=915&pid=405301&page=1&extra=page%3D1#pid405301]qqpjyj They didnt know anybody in Manchester[/url]
    [url=http://www.busyart.com/en/boards/topic/1161581/vtzwsq-there-are-18-holes-among-the-grazing-sheep]vtzwsq there are 18 holes among the grazing sheep[/url]

发表评论