Math.random()
This is as simple as it gets:Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
Usage:
double d = Math.random();
Random class methods
An instance of this class is used to generate a stream of pseudorandom numbers.
Here are some of the methods:
| Method | Return type | Return value |
|---|---|---|
| Get the next value in the stream: | ||
| nextBoolean() | boolean | true or false |
| nextDouble() | double | between 0.0d (inclusive) and 1.0d (exclusive) |
| nextFloat() | float | between 0.0f (inclusive) and 1.0f (exclusive) |
| nextInt() | int | any int within int range |
| nextLong() | long | any long within long range |
| Generate a random integer in a given range: | ||
| nextInt(int n) | int | between 0 (inclusive) and the specified value (exclusive) |
Random r = new Random();
double d = r.nextDouble();
ThreadLocalRandom class methods
A random number generator isolated to the current thread.It's a subclass of java.util.Random, so all methods that are in Random can be used here too.
Get the instance with
ThreadLocalRandom.current()| Method | Return type | Return value |
|---|---|---|
| nextDouble(double n) | double | between 0 (inclusive) and the specified value (exclusive) |
| nextDouble(double least, double bound) | double | between the given least value (inclusive) and bound (exclusive) |
| nextInt(int least, int bound) | int | between the given least value (inclusive) and bound (exclusive) |
| nextLong(long n) | long | between 0 (inclusive) and the specified value (exclusive) |
| nextLong(long least, long bound) | long | between the given least value (inclusive) and bound (exclusive) |
double d = ThreadLocalRandom.current().nextDouble(0.0, 1.0);
No comments:
Post a Comment