This question already has answers here:
Java random numbers using a seed
(7 answers)
Closed 5 years ago.
If I set seed in Random why always get same random number in below code:
private static void createArray(int[] x) {
for(int i =0; i<x.length; i++){
Random random = new Random(500l);
x[i] = random.nextInt(100000); //53695
}
}
I am getting 53695 for every run and entire loop.
Because that's what happens when you use the same seed in a pseudo-randomnumber generator. It's not random, it just looks "random enough", but it's all thanks to a deterministic mathematical formula.
Use SecureRandom if you need better randomness.
Here are some examples of seeds that provide "interesting" "random" numbers:
http://insights.dice.com/2014/01/24/generating-random-numbers-javas-random-class/
Related
This question already has answers here:
Why this code is giving strange result? So Random?
(6 answers)
Why random is behaving differently here?
(3 answers)
Closed 5 years ago.
There is interesting quiz which I didn't find answer yet
Random random = new Random(-6732303926L);
for (int i = 0; i < 10; i++) {
int randomNumber = random.nextInt(10);
System.out.print(randomNumber);
}
OUTPUT: 0123456789
My question is not about why random generates the same output all time. It's clear from documentation and, for example, from this answer.
My question is why it generates ordered numbers?
Could someone describe how does it work?
This question already has answers here:
Math.random() versus Random.nextInt(int)
(4 answers)
Closed 7 years ago.
The Math class in Java has a method, Math.random() which returns a pseudorandom number between 0 and 1.
There is also a class java.util.Random which has various methods like nextInt(), nextFloat(), nextDouble(), nextLong()etc.
My question is that if I want to get a random number in a range (say, 30-70), then which way should I go? The factors under consideration are speed and randomness.
If you look at the implementation of Math.random(), you'll see that it uses an instance of the Random class :
public static double random() {
return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
}
private static final class RandomNumberGeneratorHolder {
static final Random randomNumberGenerator = new Random();
}
Therefore the randomness would be the same.
That said, since you need an int and not a double, you'd better use the nextInt method of the Random class, since it would save you the multiplication and casting of a double to int.
Random rnd = new Random();
int num = rnd.nextInt(41)+30;
I personally would go with the random class, because, in my opinion, it's way easier to use and to influence with parameters, for example for a random number between 30-79
Random r = new Random(); int i = r.nextInt(40)+30;
I hope that helped at least a bit
Math.random() does rely on the Random class. Depending on the case and data type you are using you can use either. For an int Random.nextInt() would probably be the best choice.
Speaking of randomness, a more secure random class is SecureRandom. SecureRandom does not rely on the system time unlike Random, so if your application has a security element to it definitely use SecureRandom. Same method names, except
SecureRandom secureRandom = new SecureRandom() instead of
Random random = new Random().
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 9 years ago.
So I am trying to generate a random number, but I can't use the Java random function because I need the numbers to be in the range of 1-25. What is the easiest aka most efficient way of doing this? If possible, an explanation would be great!
int random = (int)(Math.random() * 25 + 1);
or
int random = new Random.nextInt(24) + 1;
I prefer to use the Java Random Class.
import java.util.Random;
And then generate the random nuber like this - assuming you want an integer:
Random gen = new Random();
int r = gen.nextInt(25) + 1;
Random numbers between 1 and 25 (1-25)
Random.nextInt(24) + 1
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generating random number in a range with Java
I am trying to create a random number that is above the value of one boundary, and below another. The number can be equal to either of the boundaries too.
Both boundaries are created from random. highNumber is a random between 0 and 100, and lowNumber is a random between 0 and half of highNumber.
At the moment my code is as follows:
public static void createCorrectNumber() {
random = new Random();
correctNumber = random.nextInt(highNumber)+1;
correctNumber -= lowNumber;
}
This is not functional, as when the lower bound is taken away from it, it can become lower than the boundary. Any ideas?
use
correctNumber = random.nextInt(highNumber - lowNumber + 1) + lowNumber;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java random always returns the same number when I set the seed?
Java Random Numbers Using a Seed
Hi,
This is my code. I am trying to generate 2 random numbers simultaneously using a seed i.e. 15416640. The numbers that are getting generate are not really random.
Random radiusGenerator = new Random(15416640);
Random angleGenerator = new Random(15416640);
try
{
for(int i=1; i<=sequenceNumber; i++)
{
double radius = (0.5 - (0.5 * Math.sqrt(1-radiusGenerator.nextDouble())));
double angle = angleGenerator.nextDouble();
angle = angle*(Math.PI*2);
System.out.print(radius+" "+ angle +"\n");
}
Please Help...Thanks!
That's totally normal and a feature : in a Pseudo Random Generator, the seed defines the sequence of numbers that will be generated.
Use one Random object, and generate everything you want. Since you initialize 2 Random object with the same seed, they will generate the same number if you call with the same method.