Random Number with Java [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 really want some Java that randomizes from 1-2 and all that I came up with (Doesn't work either) is:
random.math (int.1+2)
Might actually look stupid to an expert but yeah

Since you're just wanting to flip between two integers, You can utilize a ternary operator and the Math.random() static method to achieve the desired results:
Math.random() >= 0.5 ? 2 : 1

The easiest would be to use the Random.nextInt(int n) method, which will generate an integer between 0 and n-1, inclusive.
Here is an example:
Random rnd = new Random();
for (int i = 0; i < 20; i++) {
int number = rnd.nextInt(2) + 1;
System.out.print(number + " ");
}
OUTPUT
1 2 1 1 2 1 1 2 2 1 2 2 2 2 1 2 2 2 1 1

Related

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;

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

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

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