Your formula generates numbers between min and min + max.
The one Google found generates numbers between min and max.
Google wins!
Answer from pjz on Stack OverflowRandom numbers with Math.random() in Java - Stack Overflow
How does Math.random() EXACTLY work in Java? - Stack Overflow
[Java] How does Math.random() generate a random number?
Do you guys use Math.random() or the Random class more often for generating random numbers?
Videos
Your formula generates numbers between min and min + max.
The one Google found generates numbers between min and max.
Google wins!
A better approach is:
int x = rand.nextInt(max - min + 1) + min;
Your formula generates numbers between min and min + max.
Random random = new Random(1234567);
int min = 5;
int max = 20;
while (true) {
int x = (int)(Math.random() * max) + min;
System.out.println(x);
if (x < min || x >= max) { break; }
}
Result:
10
16
13
21 // Oops!!
See it online here: ideone
I was however, puzzled why a random number (say 0.78396954122) multiplied by 6 could become a random number between 1 to 6. Supposedly the random number 0.78396954122 multiplied by 6, is always 6, right?
No. 0.78396954122 * 6 is 4.70381724732 which when truncated by (int) becomes 4 which then becomes 5 when the + 1 is done.
Similarly, Math.random might return 0.2481654 which when multiplied by 6 is 1.4889924, which (int) truncates to 1, and then becomes 2 thanks to the + 1.
If it were, say, 0.0485847 * 6, that would be 0.2915082, which (int) truncates to 0, which becomes 1 thanks to the + 1.
The lowest value Math.random will ever return is 0.0, which is truncated to 0 by (int). 0 * 6 is 0, which becomes 1 after the + 1. The highest value Math.random will ever return is 0.99999999(etc) (e.g.,. it will be < 1). Multiplying that by 6 gives us 5.999999(etc) which is truncated to 5 by (int), and then turned into 6 by + 1.
So that's how we get the range 1-6 from that code.
Math.random()
It generates a random number between 0.0 to 1.0(always less then 1.0).So,in your problem as you said that you want to generate random number between 1 to 6 that is why you have multiplied it by 6, so that any random number the function will return will be multiplied by 6 and then + 1 ,so that it always stay between 1-6 .
And like you said , 0.78396954122 * 6 is 6 then you are wrong in calculation here,
0.78396954122 * 6 is 4.7 which will be rounded off to 4 by the (int). This is called casting in java,and at last it will become 4+1=5
That is why you get a number between 1-6 by this
I've looked up the source code and this was the purpose statement:
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range. When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random() This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else. This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.
What does this mean? How does it return a different double each time? I've always been incredibly curious as to how random number generators worked.