Languages
[Edit]
EN

Java - generate unique random numbers

3 points
Created by:
Inferio
328

There is couple of ways to generate unique random numbers in java.

1. List with Collections.shuffle

Edit
Collections.shuffle(list) - java docs:
Randomly permutes the specified list using a default source of randomness. 
All permutations occur with approximately equal likelihood.


2. HashSet with random numbers between 1 and 20

Edit

We generate new unique numbers until HashSet has expected size.

3. Java 8 - Random + IntStream + distinct + limit

Edit

ints(1, 20) - generate random numbers between 1 and 20
limit(10) - size of our output collection will be 10

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - random numbers

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join