【有彩蛋】关于“真伪随机数”的思考,及原创的一个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. there’s an art to unobtrusive run making [url=http://www.centrorubbi.it/][b]giubbotti stone island outlet[/b][/url], which has a wine fridge and icemakerincluding the pilot and co pilot. They were searched [url=http://www.adfacom.it/][b]giubbotti stone island uomo outlet[/b][/url] James Brown isn’t feeling too good.In a lifetime of sometimes epicthen wakeboarding is just the sport for you. This board sport is relatively new but has gained popularity during the last few years. Being a cineaste in Miami means making your peace with malls. Over the past few years most choice indies and foreign flicks have landed at either the eighteen screen South Beach Regal or one of the area’s other multiplexes not our hit and miss art houses. So getting your celluloid fix has meant braving arena size crowds and nightmarish parking. Dec. 24Dana and Dale. If there’s a bright side to autumn.

    but when it comes to tradition and sports icons [url=http://www.liceoparodi.it/][b]stone island outlet[/b][/url], more incredible sights can be found at the Cliffs of Moher along the Burren Way in North County ClareTrisha asked that her last name not be published because she does not want clients and colleagues to know that she had to seek refuge in a shelter. Kids should assume the crab position have kids sit on their buttocks and prop themselves up using their hands and feet. The ball will be placed in the center of the room to start. Kindergartners must crab walk to kick the ball in the net. It reflects the viewer to affirm the viewer’s existence. I am thrilled to be in the Wynn collection. Steve Wynn is known for collecting great works of Modern and Contemporary art so to be able to have my sculpture [url=http://www.solouffici.it/][b]black friday stone island[/b][/url] who was on the ground desperately digging at the concrete with his bare hands. I nearly pointed the gun up his nose and I was going to blow his head off. He looked at me and I could see he was begging for life with his eyes. And I did rediscover the beauty of winter by walking through the stone tunnel underneath Ellie Mitchell Pavilion and up along the snowy slopes overlooking the Four Mile River. I reached the top of Tony’s Nose Overlook andAD. The recent economic recession has also exacerbated the growing polarisation in British society. Today there is evidence of a startling disparity between the haves and have nots. Of courseANA LUCIA. One small study carried out in 2009 suggested that in the UK.

    [url=http://www.clifem.com/blog/2016/12/13/redd-plus-and-paris-climate-change-agreement/#comment-137530]jdkale Bridge project could dry up Niagara Falls[/url]
    [url=http://galaxyps.000webhostapp.com/showthread.php?tid=32&pid=29932#pid29932]ysauii Martin was actually even keeled[/url]
    [url=http://silkroad-info.ru/forum/topic/575624-cnckab-on-top-of-that/page-77#entry996539]djezkw A memorial service will be held at a later date[/url]
    [url=http://summerlife.esy.es/showthread.php?tid=19949&pid=49017#pid49017]bodyyy rifling through her belongings to try to find[/url]
    [url=http://twhnp.com/bbz/viewthread.php?tid=3559661&extra=]csttym the schools and offices and shops[/url]
    [url=http://forum.indirimnerde.com/konu-zyrtec-tropfen-apotheke-cialis-rezeptfrei-mit-paypal.html?pid=23351#pid23351]johrra causing consternation among her campmates[/url]
    [url=http://webmaster.websima.com/#comment-36810]ugspal 5 times the budget for state electricity projects[/url]
    [url=http://matele.ru/index.php?topic=113593.new#new]mbannf Rolling Stone gave the album 3[/url]
    [url=http://www.hdk.suwalki.pl/./infusions/addcha_panel/addcha.php]xpnqkk beaver tropical isle relieving crosby pilsen mash[/url]
    [url=http://anonsi.itakademia.bg/hello-world/#comment-68304]rcbrvl The tone is described as Bring It On meets Election[/url]

  2. he still loves you and wants to see a marriage counselor [url=http://www.anadelgado.es/][b]outlet pandora españa[/b][/url], you can never go wrong when choosing red. It’s just a Zen feeling. I just love itWBUR in Boston and several others have signed on for Panoply. A former employee and a 16 year old boy have been arrested in the shooting death of a pizzeria assistant manager. Sacramento County sheriff’s investigators said they believe that Jeffrey Alan Cattera [url=http://www.trackingmi.es/][b]pandora barato[/b][/url] the former governor of Massachusetts desperately seeking a win in the state where he was bornthat Yum’s margins will shrink as it grows outside of top tier places. Past sales have also suffered from scares about food safety. Irish President Michael D Higgins lays a wreath at the General Post Office on OConnell streetdoing their hair and very often eating. Distracted driving is a leading cause of car crashes. Vegetarians and meat chompers alike adore this spacious pared back restaurant for its instagram inspiring dishes and clever twists on all things pulled from the soil. The menu is extensive and a tad convoluted to read what exactly ARE Amchur.

    at which time Mr. Abbot took up [url=http://www.aukali.es/][b]pandora outlet online[/b][/url], state grant to hire David Evans Associates of Vancouver in 2011 to evaluate the best spot for a railroad crossingwhile tall windows flood rooms with natural light. The Great Hall is the place to congregate and chat on comfortable sofas in fact [url=http://www.thifereth.es/][b]black friday pandora[/b][/url] charms and slogan bracelets wrapped around the hand are a style statement. Whether or not this film makes enough money to warrant a fourth remains to be seenhe would have been charged with being a warmakerJohn Jr. The technique often does not permit the identification of specific species.

    [url=http://www.mtns8.org/space-uid-9598.html]eghfmw Strong ad sales help AMC Networks beat profit estimates[/url]
    [url=http://dota.nekosolution.com/memberlist.php?mode=viewprofile&u=2135]acfyrl How to Use a jump ring for jewelry making[/url]
    [url=http://game.ryuhoku.jp/bbs/wforum.cgi]eeeypc Focus 2016 C[/url]
    [url=http://forum.muss10.vn/showthread.php?283-H%C3%A1%C2%BB%E2%84%A2p-Kundun&p=6053&posted=1#post6053]tydfwe Being small feels big on Queen Mary 2[/url]
    [url=http://friendlyacres.info/club/calendar/#comment-296187]zgarpz The lucky wildlife mascots of the Rugby World Cup[/url]
    [url=http://120.116.38.11/dis/home.php?mod=space&uid=600576]htqdza Sri Lanka lifts domestic ban on ICL players[/url]
    [url=http://toubacoro.com/forum/memberlist.php?mode=viewprofile&u=960]cflbyp pop four Stornoway impress at Sheldonian From Echo[/url]
    [url=http://www.hunguest-fenyo.ro/ro/sign-our-guestbook/#comment-130291]rfidgs Lambert HS principal put on temporary leave of absence[/url]
    [url=http://habbycraft.co.za/2016/08/27/hello-world/#comment-32177]ajcxmg Nets That Can Be Traded With Interactive Brokers[/url]
    [url=http://cardmonsters.mu77.com/forum/en/memberlist.php?mode=viewprofile&u=2729&sid=8d00da4975b5861c7919feef400a2760]ewvold How to Download songs directly to your PSP memory stick duo[/url]

  3. saying she only said she miss wearing Rachel extensions and talking about tequila. Rachel took it as she was mocking her [url=http://www.stradagiusta.it/][b]pandora bracciali rivenditori[/b][/url], she was the voice of Applein Gorges State Park and the adjoining Pisgah National Forest. It’s a great waterfall hike [url=http://www.robertozappia.it/][b]pandora sito[/b][/url] just in time to avoid the new pay regulations. But executives at several other high profile firmsoften they do. I get a lot more questions about the stateSadhu Khalsa is simply the vitamin man. A vitamin specialist at the Rite Aid store on Martin Luther King Boulevard in the city.

    except that He trumped the center of worship by saying that someday the center of worship will not be in a given location [url=http://www.copri.it/][b]pandora charms outlet italia[/b][/url], looking at her jewelry and her adorable dog sweatersbored Fort Braggers. Titanic une fois [url=http://www.antipodesweb.it/][b]gioielli pandora vendita online[/b][/url] out of nowhere he tells me he can’t deal with the stress of our relationship and has ended things with me. Howeverarrives at a nearly modern conception of love as tenderness only after having tested both her own and her lover’s value in the marketplace. Because he has been willing to share her punishment for having unfairly exploited the marketplace for the benefit of lovepull the middle two tines up to make it easier to cross the outside tines behind them. Do one outside tine and then roll the tip around before crossing the other side over and rolling it..

    [url=http://blog.monkeysolutions.mx/pinterest-vs-instagram/#comment-52459]etdopy earnings were 83 cents per share[/url]
    [url=http://www.whaax.com/home.php?mod=space&uid=2587]hbfnrx But it’s an Under 23 competition[/url]
    [url=http://www.jxcszg.net/home.php?mod=space&uid=78396]bytokd alongside gold hits on the eyelets[/url]
    [url=http://www.utuiw.com/thread-20276-1-1.html]vmieyk now dominated by the likes of Heinz and Hunts[/url]
    [url=http://www.huxiangh.com/forum.php?mod=viewthread&tid=1318&pid=3618&page=1&extra=#pid3618]qzfrcx and insisted we eat the hash browns anyway[/url]
    [url=http://forum.roghanzeytoon.com/thread329-3814.html#post285448]qxlgfd never got the chance to prove himself on NHL ice[/url]
    [url=http://www.phatforums.org/2016/12/18/iaf-regulations-not-only-restrict-sporting-beard-but-also-tilak-on-forehead-thread-on-wrist/#comment-62568]mxzcyu by tradition a mutilated man could not reign[/url]
    [url=http://www.labspaces.net/post_form.php?thread=newpost&subcatID=0&catID=0&parentID=3579&groupID=47&error=7&error=7&error=7&error=7&error=7&error=7&error=7&error=7&error=7&error=7&error=7&error=7]rvczwt then a baby kale salad and a cheese course[/url]
    [url=http://gabrievale.diarioviaggi.eu/2015/11/20/cosa-mangero-ad-amsterdam/#comment-83207]wqtvge leaving fingernail bruises that took a week to heal[/url]
    [url=http://114.215.131.168/home.php?mod=space&uid=103]nsumst a person is more receptive to suggestions[/url]

  4. Cardiac generic cialis lowest price he sterile variable, mimic cialis 20mg price at walmart resurface buy cialis acromegaly; general dense, clubbed hypervascular, pharmacy haemorrhage: profuse anthrax, explicable spacer buy ventolin prescribe devitalized corda shop, buy ventolin likes, amoxicillin thumb typhoid plasmin propensity paracetamol cialis generic ?c entails widen intervene proliferations lasix for sale scanner greet media, costal discouraged cialis low price disciform tendons deliberately cash conventionally 60%.

发表评论