Alex
Alex

Reputation: 3181

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

Upvotes: 18

Views: 179121

Answers (11)

user6232985
user6232985

Reputation:

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

Upvotes: 1

Jason Chi
Jason Chi

Reputation: 21

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!

Upvotes: 2

millimoose
millimoose

Reputation: 39950

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.

Upvotes: 2

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.

Upvotes: 2

Andrew Cooper
Andrew Cooper

Reputation: 32576

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.

Upvotes: 0

manmathan
manmathan

Reputation: 41

Google is right :-)

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

Upvotes: 4

Gordon Gustafson
Gordon Gustafson

Reputation: 41209

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

Upvotes: 5

Mark Byers
Mark Byers

Reputation: 838186

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

Upvotes: 11

pjz
pjz

Reputation: 43057

Your formula generates numbers between min and min + max.

The one Google found generates numbers between min and max.

Google wins!

Upvotes: 17

F.J
F.J

Reputation: 243

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

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

Upvotes: 6

NPE
NPE

Reputation: 500347

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.

Upvotes: 3

Related Questions