Using system time directly to get random numbers

Posted by Richard Mar. on Stack Overflow See other posts from Stack Overflow or by Richard Mar.
Published on 2011-01-08T20:31:58Z Indexed on 2011/01/08 20:53 UTC
Read the original article Hit count: 198

I had to return a random element from an array so I came up with this placeholder:

return codes[(int) (System.currentTimeMillis() % codes.length - 1)];

Now than I think of it, I'm tempted to use it in real code. The Random() seeder uses system time as seed in most languages anyway, so why not use that time directly? As a bonus, I'm free from the worry of non-random lower bits of many RNGs. It this hack coming back to bite me? (The language is Java if that's relevant.)

© Stack Overflow or respective owner

Related posts about time

Related posts about random