java - What is random seed about? -
for example code below. has random class. produce same output everywhere . in case item seed?
source: link
import java.util.random; public class randomtest { public static void main(string[] s) { random rnd1 = new random(42); random rnd2 = new random(42); system.out.println(rnd1.nextint(100)+" - "+rnd2.nextint(100)); system.out.println(rnd1.nextint()+" - "+rnd2.nextint()); system.out.println(rnd1.nextdouble()+" - "+rnd2.nextdouble()); system.out.println(rnd1.nextlong()+" - "+rnd2.nextlong()); } }
42 seed, same javadoc says. so, seed? random number seldom truly random - it's pseudo-random instead. means it's generated function, said prng (pseudo random number genrator). being generated function, in turn, means output not random anymore, since it's predictable!
however, depending on needs, pseudo-randomness may enough - said enough because generating random bit expensive, , i'm not talking time or memory, money (see this link on wikipedia). so, example, if need random value place enemies in game, pseudo-random number ok - if building security-related software, want use true random number, or @ least cryptographically secure prng.
how can describe prng, 1 used in math.random()? it's function, initialized seed s returns array of values a. note that, each integer s, defined 1 , 1 array a. example (values not actual):
first call second call third call seed: 14329 .18 .82 .5 seed: 3989 .7 .02 .93 so seed prng known value when want result predictable - example testing purposes or ensure that, each time run level 1 in game, enemies placed in same (pseudo) random places - otherwise don't need explicitely pass seed.
Comments
Post a Comment