Computer Science Canada

random integers

Author:  Prince [ Thu Feb 19, 2004 11:07 am ]
Post subject:  random integers

how do u get them? i had sum code but it disappeared Confused

Author:  wtd [ Thu Feb 19, 2004 7:18 pm ]
Post subject: 

code:
import java.lang.*;
import java.io.*;
import java.util.*;

class Test
{
   long randomNumberSeed = System.currentTimeMillis();
   Random randomNumberGenerator = new Random(randomNumberSeed);
   int randomInt = randomNumberGenerator.nextInt();
}


See the Sun Java docs for the Random class.

Author:  Prince [ Fri Feb 20, 2004 10:18 am ]
Post subject: 

isnt there a simpler way Confused

Author:  Tony [ Fri Feb 20, 2004 2:12 pm ]
Post subject: 

looks preaty simple to me... once you declear your randomNumGen, its just
code:

randomNumberGenerator.nextInt();

Author:  rizzix [ Fri Feb 20, 2004 5:08 pm ]
Post subject: 

even simpler? hmm...
code:

Math.random();

Author:  wtd [ Fri Feb 20, 2004 6:18 pm ]
Post subject: 

rizzix wrote:
even simpler? hmm...
code:

Math.random();


Yeah, I knew someone would beat me to that answer, so I posted the other answer. Wink

Really, though, use either one, but know the Random class. It's more powerful and flexible than Math.random().


: