Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the seed value passed to Java's random number generator effect its output?

Tags:

java

random

How does the seed value passed to Java's random number generator effect its output? How would i go about figuring out which numbers it will output if i know the seed value?

Also are some seed values better than others at producing more psudo-randomness than others?

like image 555
namen Avatar asked May 15 '11 07:05

namen


1 Answers

You cannot generate truly random numbers in software, because software is deterministic: given some input, it will in principle always generate a predictable output.

So, to get random numbers, a number of algorithms have been invented that generate sequences of numbers that look random (but are not really - that's why they are called pseudo-random numbers).

Such an algorithm starts with some start value, the seed, and then does some calculations with it to generate the next pseudo-random number.

If the algorithm is any good, then there should be no difference in seed values: one seed value should not be better than any other in generating random numbers.

Often, the current time is taken as the seed value, so that each time you generate a sequence of numbers, you get a different sequence. Note that if you use the same seed value, you will get the same sequence of pseudo-random numbers each time you run it.

If you use pseudo-random numbers for cryptographic purposes, you should be very careful, because if an attacker knows the seed value then he can re-generate the sequence of random numbers which might compromise the security of your system. For really secure systems, people use special hardware-based random number generators, which can generate truly random numbers. Java has a class java.security.SecureRandom to interface with such systems.

See Random number generation on Wikipedia for a lot more detail and information on different algorithms.

like image 86
Jesper Avatar answered Oct 28 '22 10:10

Jesper