Random.nextInt(100); 

It seems like a normal code, right? Well, it takes this random number from 0 to 100 and figs with it. Have you ever thought how it works? If there is no sequence, then what is behind this method, nextInt ? Is this a secret or has everyone known for a long time, but am I the most so backward? Who is in the subject, enlighten me, please. I wonder can not.

  • 2
    Why think and what secret can there be when there is the source code of the class Random - pavlofff

1 answer 1

I don’t know how specifically this is implemented in Java, but in general methods of generating random numbers are, of course, not a secret. To begin with, a rapidly changing value or a combination of such values ​​is taken from the system (for example, the current time in milliseconds xor amount of free memory in bytes), some fast hash function is taken from it and the result is used as a basis (seed) for pseudo-random sequence generation. The easiest way to calculate it is: rand[i] = (rand[i - 1] * a + b) % c , where a, b and c are specially selected, fairly large numbers. It seems that modern processors are able to do this at the hardware level. See more on wikipedia: https://ru.wikipedia.org/wiki/ Pseudo-random number generator