World's most popular travel blog for travel bloggers.

[Solved]: What's the problem of using the clock to generate random numbers?

, , No Comments
Problem Detail: 

If the clock shows 14:15:36.909302, why not just use the fractions of a second part (09302) as a kind of random number?

What is wrong with this form of generating random numbers?

I am aware that obtaining truly random numbers is a difficult task, so I am assuming that there is something wrong with this method.

Asked By : Quora Feans

Answered By : Guy Coder

Quick answer:

  1. If the run time of your algorithm to pick a random number is not random you could get a pattern of times which is not random.

    As an example. Suppose the function to read the time is triggered by an event, and the event is timed to the clock, now your functions based on the clock.

  2. If the run time of your algorithm is faster than the precision of the clock, then you will get duplicate times and thus duplicate numbers which are not random.

    As an example. You write a function for use by others, and someone decides to generate 10,000 random numbers and store them. The loop is so tight for reading the clock and saving the values that you get 10,000 numbers all identical.

These are just cases that can make the data non-random. In reality if it is not security related I run a large sample through a test for randomness and if it were reasonable I would use it, but if it is security related I would ask at Security or Crypto

Best Answer from StackOverflow

Question Source : http://cs.stackexchange.com/questions/19421

0 comments:

Post a Comment

Let us know your responses and feedback