How Java random generator works?

前端 未结 4 1154
天涯浪人
天涯浪人 2021-01-04 09:36

I wrote program that simulates dice roll

    Random r = new Random();
    int result = r.nextInt(6);
    System.out.println(result);

I want

4条回答
  •  清酒与你
    2021-01-04 10:24

    Check How does java.util.Random work and how good is it?:

    In other words, we begin with some start or "seed" number which ideally is "genuinely unpredictable", and which in practice is "unpredictable enough". For example, the number of milliseconds— or even nanoseconds— since the computer was switched on is available on most systems. Then, each time we want a random number, we multiply the current seed by some fixed number, a, add another fixed number, c, then take the result modulo another fixed number, m. The number a is generally large. This method of random number generation goes back pretty much to the dawn of computing1. Pretty much every "casual" random number generator you can think of— from those of scientific calculators to 1980s home computers to currentday C and Visual Basic library functions— uses some variant of the above formula to generate its random numbers.

    And also Predicting the next Math.random() in Java

提交回复
热议问题