Random generation in Java [duplicate] - java

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

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

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 with Java [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 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

Random number between 80 and 120 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 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

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.

Categories

Resources