简体   繁体   中英

Random numbers with Math.random() in Java

For generating random numbers, I've used the formula:

(int)(Math.random() * max) + min

The formula I find on Google always seem to be:

(int)(Math.random() * (max - min) + min)

Which one's right? As far as I know, I've never gotten a number that was out of my range with my formula

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

Yours: Lowest possible is min, highest possible is max+min-1

Google: Lowest possible is min, highest possible is max-1

The first one generates numbers in the wrong range, while the second one is correct.

To show that the first one is incorrect, let's say min is 10 and max is 20. In other words, the result is expected to be greater than or equal to ten, and strictly less than twenty. If Math.random() returns 0.75 , the result of the first formula is 25 , which is outside the range.

if min=10 and max=100 :

(int)(Math.random() * max) + min        

gives a result between 10 and 110, while

(int)(Math.random() * (max - min) + min)

gives a result between 10 and 100, so they are very different formulas . What's important here is clarity, so whatever you do, make sure the code makes it clear what is being generated.

(PS. the first makes more sense if you change the variable 'max' to be called 'range')

Google is right :-)

Google's formula creates numbers between: min and max Your formula creates numbers between: min and (min+max)

int i = (int) (10 +Math.random()*11);

this will give you random number between 10 to 20.

the key here is:

a + Math.random()*b

a starting num (10) and ending num is max number (20) - a (10) + 1 (11)

Enjoy!

If min = 5 , and max = 10 , and Math.random() returns (almost) 1.0, the generated number will be (almost) 15, which is clearly more than the chosen max .

Relatedly, this is why every random number API should let you specify min and max explicitly. You shouldn't have to write error-prone maths that are tangential to your problem domain.

There's a small problem with the formula that you found on Google. It should be:
(int)(Math.random() * (max - min + 1) + min)
not
(int)(Math.random() * (max - min) + min) .

max - min + 1 is the range in which random numbers can be generated

Math.random()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

Now it depends on what you want to accomplish. When you want to have Numbers from 1 to 100 for example you just have to add

(int)(Math.random()*100)

So 100 is the range of values. When you want to change the start of the range to 20 to 120 you have to add +20 at the end.

So the formula is:

(int)(Math.random()*range) + min

And you can always calculate the range with max-min, thats why Google gives you that formula.

Math.random() generates a number between 0 (inclusive) and 1 (exclusive).

So (int)(Math.random() * max) ranges from 0 to max-1 inclusive.

Then (int)(Math.random() * max) + min ranges from min to max + min - 1 , which is not what you want.

Google's formula is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM