简体   繁体   中英

Create 2 arrays of random integers

I'm trying to create 2 arrays of random integers in the range 1-6. But I keep getting the same numbers.

Random random = new Random();
for (int i = 0; i < 5; i++) {
    player1[i] = random.nextInt(6) + 1;
    player2[i] = random.nextInt(6) + 1;
}

your code should return what's expected.

I would use this rather.

Min + (int)(Math.random() * ((Max - Min) + 1))

random number generated from above will be Min inclusive and Max Exclusive

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