I need help width the Random code [duplicate] - java

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;

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))

arc4random equivalent for 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.
using iOS I sometimes like to call 2 functions at random a button press for example, I would use; So sometimes, the user would get (1) the other time (2) etc.
if (arc4random() % 2 == 0) {
// Do one thing (1)
} else {
// Do something else(2)
}
}
How would I do this within Eclipse/java? In otherwords, what is the same statement but in a Java language?
Use the Java Random class. This would give you either 1 or 2:
Random rand = new Random();
int n = rand.nextInt(2) + 1;
nextInt(n) gives you a random number from 0 to n-1 (inclusive). So you have to add 1 to the result.

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;

Specify max and min for Random.nextInt()? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Java: generating random number in a range
I want to generate a random int in a logical range. So, say for example, I'm writing a program to "roll" a dice with a specified number of sides.
public int rollDice() {
Random generator = new Random();
return generator.nextInt(sides);
}
Now the problem becomes that this will return values between sides and zero, inclusive, which makes no sense because most dice go from 1 to 6, 9, etc. So how can I specify that nextInt should work between 1 and the number of sides?
To generate a random int value (uniform distribution) between from and to (inclusive) use:
from + rndGenerator.nextInt(to - from + 1)
In your case (1..sides):
1 + rndGenerator.nextInt(sides)

Categories

Resources