This question already has answers here:
Why this code is giving strange result? So Random?
(6 answers)
Why random is behaving differently here?
(3 answers)
Closed 5 years ago.
There is interesting quiz which I didn't find answer yet
Random random = new Random(-6732303926L);
for (int i = 0; i < 10; i++) {
int randomNumber = random.nextInt(10);
System.out.print(randomNumber);
}
OUTPUT: 0123456789
My question is not about why random generates the same output all time. It's clear from documentation and, for example, from this answer.
My question is why it generates ordered numbers?
Could someone describe how does it work?
Related
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))
This question already has answers here:
Java random numbers using a seed
(7 answers)
Closed 5 years ago.
If I set seed in Random why always get same random number in below code:
private static void createArray(int[] x) {
for(int i =0; i<x.length; i++){
Random random = new Random(500l);
x[i] = random.nextInt(100000); //53695
}
}
I am getting 53695 for every run and entire loop.
Because that's what happens when you use the same seed in a pseudo-randomnumber generator. It's not random, it just looks "random enough", but it's all thanks to a deterministic mathematical formula.
Use SecureRandom if you need better randomness.
Here are some examples of seeds that provide "interesting" "random" numbers:
http://insights.dice.com/2014/01/24/generating-random-numbers-javas-random-class/
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;
}
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.
This question already has answers here:
Is Integer Immutable
(12 answers)
Closed 9 years ago.
I know Integer is immutable in Java. But I tried this:
Integer i = 4;
i++;
System.out.println(i); // output is 5
Why the self-increment is still working? Did Java create a new Integer object?
i++;
is equivalent to:
i = i + 1;
which i now refers to a different Integer object (5).