简体   繁体   中英

j2me - random number - card game

I am trying to generate 5 different random numbers for use in my card game app, so far i am using a while loop as follows which prints out 5 of the same number, i am wondering how to make 5 DIFFERENT random numbers, surely it cant be much different to the code i have so far....

 int n = 0;

 while(n<5)

{

 Random r = new Random();

 int i = r.nextInt(10);

 System.out.println( i);  

 n++;

 }

I hope somebody can help:-)

x

Try moving Random r = new Random(); outside of your while loop.

The seed is based on the timestamp:

Two Random objects created within the same millisecond will have the same sequence of random numbers.

(reference)

Since you're not doing much inside your loop it's not taking more than a millisecond between calls, which means that each new Random() is initialized with the same seed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM