How does a random number generator work?

前端 未结 7 1308
终归单人心
终归单人心 2020-11-29 05:27

How do random number generator works? (for example in C/C++ Java)

How can I write my own random number generator? (for example in C/C++ Java)

7条回答
  •  感动是毒
    2020-11-29 05:58

    There is also this algorithm:

    enter image description here

    Oh, and more seriously:

    Random number generators use mathematical formulas that transfer set of numbers to another one. If, for example, you take a constant number N and another number n_0, and then take the value of n mod N (the modulo operator), you will get a new number n_1, which looks as it if is unrelated to n_0. Now, repeat the same process with n_1 and you'll get another number. What you have here is a (VERY BAD) generator of seemingly random numbers.

    Remember, the method I've described here is a toy method that should not be used for anything serious. It does, however, illustrate the general principle.

    Also note:

    If all scientific papers whose results are in doubt because of bad rands were to disappear from library shelves, there would be a gap on each shelf about as big as your fist.

    (paraphrased from chapter 7 of Numerical recipes). This is a must-read text for anyone who uses random number generators for any serious work.

提交回复
热议问题