开发者

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

开发者 https://www.devze.com 2023-03-06 12:33 出处:网络
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?

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-random开发者_运维问答ness than others?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消