Pseudo-random number generator

2019-01-17 17:06发布

What is the best way to create the best pseudo-random number generator? (any language works)

10条回答
Juvenile、少年°
2楼-- · 2019-01-17 17:28

See this link for the TestU01 suite of tests, which includes several batteries of tests.

http://www.iro.umontreal.ca/~simardr/testu01/tu01.html

In the paper, the author demonstrates the test result on a variety of existing RNGs, but not .NET System.Random (as far as I can tell). Though he does test VB6's generator.

Very few pass all the tests...

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-17 17:32

Best way to create one is to not to.

Pseudo-random number generators are a very complex subject, so it's better off to use the implementations produced by the people that have a good understanding of the subject.

查看更多
孤傲高冷的网名
5楼-- · 2019-01-17 17:38

Steal the one out of knuth seminumeric. It is high quality and simple to implement. It uses a pair of arrays, addition, and a couple of ifs. Cheap, effective, and a nice long period 2^55 if i recall correctly.

查看更多
走好不送
6楼-- · 2019-01-17 17:39

Yikes, that can get VEEEEEERY complicated! There seem to be a number of metrics for how to measure the "randomness" of a random number generator, so it's difficult to meaure which are "best". I would start with Numerical Recipes in C (or whatever langauge you can find one for) for a few examples. I coded up my first simple one from the examples given there.

EDIT: It's also important to start by determining how complex you need your random number generator to be. I remember a rude awakening I had in C years ago when I discovered that the default random number generator had a period somewhere around 32,767, meaning that it tended to repeat itself periodically after generating that many numbers! If you need a few dice rolls, that's fine. But not when you need to generate millions of "random" values for a simulation.

查看更多
戒情不戒烟
7楼-- · 2019-01-17 17:40

PRNG algorithms are complicated, as is acquiring the right sources of entropy to make them work well. This is not something you want to do yourself. Every modern language has a PRNG library that will almost certainly be suitable for your use.

xkcd random number

查看更多
登录 后发表回答