有效地生成随机数 [英] Generating random numbers effectively

查看:155
本文介绍了有效地生成随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何有效地生成随机数?
每当一个随机数的程序启动时,它即开始之前吐痰相同的编号。 (我猜是因为随机数生成的准自然)
有没有一种方法,即随机#一代变得不确定性?排序熵除了代开机后产生的号码是在不同的序列比去年之一。 (随机随机而这半随机)
此外,说出这样产生范围为(M,N),使得纳米= X,是有一个机会,一个数字说P似乎已经产生的X-1等数字后,下一次。但是,接下来的很多这样的x个不会是一样的,从最后一个序列。示例:
范围:1,5。代:2,4,5,1,3(1日)4,2,3,1,5(第二)... 相同的数字
我心不烦的不知所措的状态中写道的:

How do you generate random numbers effectively?
Every time a random number program boots up, it starts spitting same numbers as before. (I guess because of quasi nature of random number generation)
Is there a way, that random# generation becomes non-deterministic? sort of Entropy addition to generation that number generated after boot is in different sequence than last one. (random random rather that quasi-random)
Also, say range of such generation is (m,n) such that n-m = x, is there a chance that a number say 'p' appears next time after x-1 other numbers have been generated. But next lot of such x numbers would not be same as sequence from last one. Example:
range: 1,5. Generation : 2,4,5,1,3 (1st) 4,2,3,1,5 (2nd)... same numbers.
I out of nonplussed state of mind wrote this :

int num1 = (rand.Next(1, 440) *31* (int)DateTime.Now.Ticks *59* (DateTime.Now.Second * 100) % 439) + 1;
int num2 = (rand.Next(1, 440) *31* (int)DateTime.Now.Ticks *59* (DateTime.Now.Second * 100) % 439) + 1;

下面范围为(1,440)。但它仍然会产生数出界和零,它的频率是不是很大要么。这是C#.net code。为什么这样?
你的答案可能是语言无关/算法/分析。先谢谢了。

here range was (1,440). but it still generates numbers out of bound and zero, and it's frequency is not that great either. It is C#.NET code. Why so?
your answers can be language agnostic / algorithmic / analytical. Thanks in advance.

推荐答案

很少有随机数生成器实际上是随机的。几乎所有的都是伪,具有相同的种子值开始时,下面一个predictable序列。许多伪随机数生成器器(PRNG)从他们的初始调用的日期和时间,他们的后裔。他人得到他们的种子从由操作系统,这往往是从外部源产生的(例如的,鼠标移动,键盘活动)供给随机数据源。

Very few "random" number generators are actually random. Almost all are pseudorandom, following a predictable sequence when started with the same seed value. Many pseudorandom number generators (PRNGs) get their seed from the date and time of their initial invocation. Others get their seed from a source of random data supplied by the operating system, which often is generated from outside sources (e.g., mouse motion, keyboard activity).

正确的方法播种一个好的随机数生成器是不是种子吧。每一位优秀的产生都有一个默认的机制来提供种子,它通常是比任何你能想出更好的。唯一真正的理由种子生成器,如果你真的要随机数相同的序列(例如的,当你试图重复这个过程需要随机性)。

The right way to seed a good random number generator is to not seed it. Every good generator has a default mechanism to supply the seed, and it is usually much better than any you can come up with. The only real reason to seed the generator is if you actually want the same sequence of random numbers (e.g., when you're trying to repeat a process that requires randomness).

请参阅 http://msdn.microsoft.com/en-us /library/system.random.aspx ,在C#Random类的细节,但基本上,它使用一个非常著名和受人尊敬的算法和种子用的日期和时间。

See http://msdn.microsoft.com/en-us/library/system.random.aspx for the details of the C# Random class, but basically, it uses a very well known and respected algorithm and seeds it with the date and time.

要回答你的关键问题,只需使用 rand.Next(最小,最大+ 1)键,你会总是得到一个随机序列在数字分钟最高的包容性。每次使用相同的种子时的顺序是一样的。但兰特=新的随机()将使用当前的时间,只要你的程序被调用,在某个时间间隔,他们会有所不同。

To answer your key question, just use rand.Next(min, max+1) and you'll always get a random sequence of numbers between min and max inclusive. The sequence will be the same every time you use the same seed. But rand = new Random() will use the current time, and as long as your program is invoked with some separation in time, they'll be different.

这篇关于有效地生成随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆