This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Math.random() explanation
(5 answers)
Closed 8 years ago.
I need to generate a random integer with Java but random in a one sided bounded specific range. For example, a range from 15+ means that the only constraint is that the lowest value the integer can take is 15.
Random rand = new Random();
int min=15;
int randomNum = rand.nextInt((2147483647 - min) + 1)+ min;
You can create a simple randomInRange function like this:
Yyou want to create the Random object only once so it doesn't have to re-seed each time you call the randomInRange() function.
Random rand;
// ...
// where you initialize stuff (for example the class constructor)
rand = new Random();
// ...
int randomInRange(int min, int max) {
return rand.nextInt((max - min) + 1) + min;
}
If you want to only have the minimum value, maybe create another method like:
int randomFrom(int min) {
return randomInRange(min, Integer.MAX_VALUE);
}
I suggest you make a separate Utilities class that will contain these methods as static and you can call them by saying Utilities.randomInRange()
You can of course make them static in your class as well if these are the only utility methods you will need
Related
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 8 years ago.
I've searched high and low for the answer to this and can't see how I'm doing it wrong, I've tried several different ways of coding it but it always goes outside the range I'm looking for. (90 - 180 inclusive).
Can anyone shed some light on the code below???
Random rnd = new Random();
int time = rnd.nextInt(91) + 90;
Thanks for any support offered...
/**
* Randomly generates a number between 90-180 inclusive and sets the random number as an argument to a message-send for each of the
* Runner objects in runnersList.
*/
public void runMarathon()
{
for (Runner r : runnersList)
{
Random rnd = new Random();
int time = rnd.nextInt(91) + 90;
r.setTime(time);
}
}
This function will help you to do what you need
private int randInt(int min, int max) {
return new Random().nextInt((max - min) + 1) + min;
}
just give min and max values as parametres to the function and you will get a random value between the range you specified ;)
A key principal in software development is reuse. So to do this, you can use
"http://commons.apache.org/proper/commons-lang/javadocs/api-3.3/org/apache/commons/lang3/RandomUtils.html#nextInt(int, int)" instead of reinventing the wheel.
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java: generating random number in a range
I need a little help.
What code would I use to create a random number that is 5 digits long and starts with either 1 or 2?
In order to use as a company employees ID?
Depending on how you approach the problem something like that:
public int gen() {
Random r = new Random( System.currentTimeMillis() );
return 10000 + r.nextInt(20000);
}
Or something like that (you probably want the instantation of the Random object of of the method but I just put it here for simplicity) :
public int gen() {
Random r = new Random( System.currentTimeMillis() );
return ((1 + r.nextInt(2)) * 10000 + r.nextInt(10000));
}
The idea is that 1 + nextInt(2) shall always give 1 or 2. You then multiply it by 10000 to satisfy your requirement and then add a number between [0..9999].
Here's are some example output:
14499
12713
14192
13381
14501
24695
18802
25942
21558
26100
29350
23976
29045
16170
23200
23098
20465
23284
16035
18628