The current random generation algorithm or not?

long timeValue = timeInMillis();
int rand = timeValue%100 + 1;

If we execute the above code N times in a loop, it will generate N random numbers from 1 to 100. I know that generating random nos is a difficult problem. Just wanted to know if this is a good random number generation algorithm? Or is it a pseudo random number generator?

Why do I think this will lead to a good assessment of random behavior? 1) anyway from 1 to 100 will be evenly distributed. No prejudice. 2) timeInMillis will exhibit somewhat random behavior, because we can never guess what time the processor will perform this function. The CPU has many tasks. Therefore, the exact execution time of the timeInMillis () command is not predictable in the next iteration of the loop.

+3
source share
2 answers

No. For starters, on most processors, this will be a loop repeatedly (possibly a full 100) for 1 millisecond, which will result in 100 identical numbers.

Even sowing a random number generator with a timer timer can be dangerous - a timer tick is rarely "random", as you might expect.

+5
source

here is my suggestion to generate random numbers:

1 select a selection of sites that are as far away as possible from your location. for example, if you are in the USA, try several sites that have the IP addresses of their servers in Malaysia, China, Russia, India. high traffic servers are better.

2- - ( 7 - 11 ) , ping ( ) 2 (.. ping : 0, 1).

3- , .

4- , (, ) . (, , , :))

: ..etc

+1

Source: https://habr.com/ru/post/1772583/


All Articles