简体   繁体   中英

How do I make a large number of random arrays?

I need to make an array that has 8000 random integers between values 0 - 65535 for a hash table assignment, I understand the math.random function but how do I get random numbers for such a large range of numbers? Thanks!

If you're in Java 8, you can write

int[] array = new Random().ints(0, 65536).limit(8000).toArray();

otherwise you'd just write something like

int[] array = new int[8000];
Random rng = new Random();
for (int i = 0; i < 8000; i++) {
  array[i] = rng.nextInt(65536);
}

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