Generating random numbers within a specific range [duplicate] - java

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.

Related

Generate random number with constraint in java [duplicate]

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

How do I have a random int from 1 to 6 using math.random() in java? [duplicate]

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'm writing a method in java which relates to a die throw and I try to use the math.random() but i realized that 0 is included in the random integers involved. Additionally, I don't quite get the *7 part what does it mean?
I went to research from the Java API but it doesn't mention any bit about this or is it I am doing the wrong research? Thanks so much for reading!
public int dieThrow()
{
int num = (int)(Math.random() *7); //returns an integer
return num;
}
This is a pretty simple exercise. You observe that 0 is a possible outcome, so you simply + 1 to the result, like so:
public int throwDie()
{
return (int)(Math.random() * 6) + 1;
}

Random number from 1 - 25 in java sequence [duplicate]

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

Creating a random integer between two random boundaries [duplicate]

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;

How to generate a random five digit number Java [duplicate]

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

Categories

Resources