Random number between 80 and 120 java [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 need to create a random number between 80 and 120, I was given the line
int tempF = (-17 + (int) (Math.random() * ((-20 - (-17)) + 1)));
and I added the line
Random generator = new Random();
above it, but I'm not sure where to go from here?

Use something like this:
Random ran = new Random();
int x = ran.nextInt(10); //where 10 is range

Related

How can I make number in Math.random come out from 2? [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 6 months ago.
How can I make the number in Math.random() come out no less than the number I need?
Cards card1 = new Cards();
card1.cardsnumber = Math.random()*21;
int card1int = (int) Math.round(card1.cardsnumber);
How can I make card1int come out from 2 to 21?
Just add 1 to the random number created and use 20 instead of 21
Cards card1 = new Cards();
card1.cardsnumber = (Math.random()*20) + 1;
int card1int = parseInt(Math.round(card1.cardsnumber))

Random generation in Java [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 4 years ago.
Is there a method like the rng.randint() in the code below for Java?
Are Math.random() o java.util.Random viable for this aim?
...
rng.seed(random_seed)
child1, child2 = parents
const1 = child1.const_set
const2 = child2.const_set
cp1 = rng.randint(1, len(const1) - 1) if len(const1) > 1 else 0
cp2 = rng.randint(1, len(const2) - 1) if len(const2) > 1 else 0
new Random().nextInt(<specify bound here>)
If bound is 5, it will give you a random int less than 5

I need help width the Random code [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 6 years ago.
I need help width the Random r = new Random(); code.
I will give you my code and tell you what I want to do.
Random r = new Random();
int i = r.nextInt(3);
System.out.println(i);
//now how do i make it not to take 0 as a random number
//One of them is 0, how do i make it take a random int that is
//bigger than 0 but lower than 3. I need help there.
Well, think about it. You currently get 0, 1, or 2. So how could you get just 1 or 2? You pass 2 into nextInt so you get either 0 or 1, and add 1 to the result:
int i = r.nextInt(2) + 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;

Categories

Resources