# Math.random()

Thomas Markl

Ranch Hand

Posts: 192

posted 13 years ago

- 0

Does this return a random value between 1 and 6 or between 1 and 7??

private int throwDice(){

return 1+(int)(Math.random()*6);

//Math.random returns a double value between 0.0 and 6.0 and cast it

//to int and adds one. So the return value of throwDice() is an int

//number between 1 and 7.

}

private int throwDice(){

return 1+(int)(Math.random()*6);

//Math.random returns a double value between 0.0 and 6.0 and cast it

//to int and adds one. So the return value of throwDice() is an int

//number between 1 and 7.

}

Dirk Schreckmann

Sheriff

Posts: 7023

posted 13 years ago

- 0

1+(int)(Math.random()*6)

Math.random() returns a number in the range [0,1). Multiply this by six and get [0,6). Cast it to int and get [0,5] which is the same as [0,6) when considering only whole numbers. Add one to it and get [1,6]. So, it returns a random number from 1 inclusive to 6 inclusive, which is not unlike the range from 1 inclusive to 7 exclusive when considering only whole numbers. The documentation lies. Who's ever seen that before?

Math.random() returns a number in the range [0,1). Multiply this by six and get [0,6). Cast it to int and get [0,5] which is the same as [0,6) when considering only whole numbers. Add one to it and get [1,6]. So, it returns a random number from 1 inclusive to 6 inclusive, which is not unlike the range from 1 inclusive to 7 exclusive when considering only whole numbers. The documentation lies. Who's ever seen that before?

John Lee

Ranch Hand

Posts: 2545

posted 13 years ago

Is this true?

for an int, [0,5] and [0,6) are the same in term of range, but the frequency of each number's appearence is not the same. I believe for a random int in [0, 6), you will more often get 5 than a random int in [0,5].

- 0

Originally posted by Dirk Schreckmann:

Cast it to int and get [0,5] which is the same as [0,6) when considering only whole numbers.

Is this true?

for an int, [0,5] and [0,6) are the same in term of range, but the frequency of each number's appearence is not the same. I believe for a random int in [0, 6), you will more often get 5 than a random int in [0,5].

Ellen Zhao

Ranch Hand

Posts: 581

posted 13 years ago

This is True. You can find the rigorous mathematical proof in some discrete mathematic theory or probability theory paper.

Random values can have certain well-known distributions. When random values are uniformly distributed between a minimum and a maximum value( for example, between 0 and 1 ), every value has an equal probability of occuring. Random values that are normally distributed have probabilities that are represented by the familiar bell curve.

A well known algorithm for generating uniformly distributed random values is the linear congruential algorithm. This algorithm generates random integer values that range over all the possible positive and negative values of the integer type. It uses the formula

X(n+1) = ( m * X(n) + a ) mod d

Where the Xi are the generated random values, m is a constant multiplier, a is a constant addend and d is a constant divisor, which is usually a very big number and relatively prime to m( I think Don must know "da4 shu4 xiang1 chu2 ding4 li3" ). A seed value kicks off the sequence. The formula relies on the fact that integer arithmetic does not overflow but wraps around.

Regards,

Ellen

- 0

Is this true?

This is True. You can find the rigorous mathematical proof in some discrete mathematic theory or probability theory paper.

Random values can have certain well-known distributions. When random values are uniformly distributed between a minimum and a maximum value( for example, between 0 and 1 ), every value has an equal probability of occuring. Random values that are normally distributed have probabilities that are represented by the familiar bell curve.

A well known algorithm for generating uniformly distributed random values is the linear congruential algorithm. This algorithm generates random integer values that range over all the possible positive and negative values of the integer type. It uses the formula

X(n+1) = ( m * X(n) + a ) mod d

Where the Xi are the generated random values, m is a constant multiplier, a is a constant addend and d is a constant divisor, which is usually a very big number and relatively prime to m( I think Don must know "da4 shu4 xiang1 chu2 ding4 li3" ). A seed value kicks off the sequence. The formula relies on the fact that integer arithmetic does not overflow but wraps around.

Regards,

Ellen

Jasper Vader

Ranch Hand

Posts: 284

John Lee

Ranch Hand

Posts: 2545

posted 13 years ago

Hi Ellen:

It seems to me that we don't even need bell curve here. Because a random number is generated uniformly in [0, 1) (by definition). So when we multiply the random number by 5 and 6, we get [0, 5) and [0, 6) respectively.

For a random number in [0, 5), assume we round the random number to closest int, same below. We have 20% chance to get 1, 2, 3, and 4. 10% to get 0 and 5.

For a random number in [0, 6), we have 16.67% chance to get 1, 2, 3, 4, and 5. 8.34% to get 0 and 6.

I think if in other rounding scheme, the result distribution should not be the same for [0, 5) and [0, 6) either.

- 0

Originally posted by Ellen Fu:

This is True.

Hi Ellen:

It seems to me that we don't even need bell curve here. Because a random number is generated uniformly in [0, 1) (by definition). So when we multiply the random number by 5 and 6, we get [0, 5) and [0, 6) respectively.

For a random number in [0, 5), assume we round the random number to closest int, same below. We have 20% chance to get 1, 2, 3, and 4. 10% to get 0 and 5.

For a random number in [0, 6), we have 16.67% chance to get 1, 2, 3, 4, and 5. 8.34% to get 0 and 6.

I think if in other rounding scheme, the result distribution should not be the same for [0, 5) and [0, 6) either.

Ilja Preuss

author

Sheriff

Sheriff

Posts: 14112

posted 13 years ago

Why? All values in [0,1) would get rounded down to 0, all values in [1,2) to 1 ... and all values in [5,6) to 5. So it seems to me that the probability for all of them would be equal.

- 0

Originally posted by Don Liu:

I believe for a random int in [0, 6), you will more often get 5 than a random int in [0,5].

Why? All values in [0,1) would get rounded down to 0, all values in [1,2) to 1 ... and all values in [5,6) to 5. So it seems to me that the probability for all of them would be equal.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

John Lee

Ranch Hand

Posts: 2545

Dirk Schreckmann

Sheriff

Posts: 7023

Dirk Schreckmann

Sheriff

Posts: 7023

posted 13 years ago

I don't think that's an unreasonable way to understand this. Note that the cast to int doesn't change the range of numbers produced before the decimal point, it just chops off whatever might have been to the right.

- 0

*why does the cast to int change the range? because of a rounding down?*

I don't think that's an unreasonable way to understand this. Note that the cast to int doesn't change the range of numbers produced before the decimal point, it just chops off whatever might have been to the right.

John Lee

Ranch Hand

Posts: 2545

posted 13 years ago

Hi Dirk:

Thanks for your response. I didn't say multiplying a uniformly distribution will get an unevenly distribution.

If we go back to the original question:

I think the answer is [1, 6]. I think you also agreed on this.

- 0

Originally posted by Dirk Schreckmann:

Don, it would seem to me that you are arguing that by multiplying a uniform distribution by some constant (so to speak), the distribution suddenly becomes unevenly distributed. Why do you think so?

Hi Dirk:

Thanks for your response. I didn't say multiplying a uniformly distribution will get an unevenly distribution.

If we go back to the original question:

Does this return a random value between 1 and 6 or between 1 and 7??

private int throwDice(){

return 1+(int)(Math.random()*6);

I think the answer is [1, 6]. I think you also agreed on this.

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 13 years ago

- 0

The answer is indeed [1, 6]. However you've also indicated you think the endpoints in this range (1 and 6) are half as likely as the other mid-range points (2, 3, 4, 5). This is incorrect - do you still believe this? Or did you follow Ilja's point about int arithmetic rounding

*down*(or more accurately, towards zero) rather than towards the nearest integer? If you still think 1 and 6 are less likely, I recommend running the method a few hunderd times and counting the results."I'm not back." - Bill Harding, *Twister*

Ilja Preuss

author

Sheriff

Sheriff

Posts: 14112

posted 13 years ago

Casting doesn't round to the nearest int - it always rounds down (at least as long as only positive numbers are involved). So we get a 20% change for 0 and a 0% chance for 5 in the original question.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

- 0

Originally posted by Don Liu:

For a random number in [0, 5), assume we round the random number to closest int, same below. We have 20% chance to get 1, 2, 3, and 4. 10% to get 0 and 5.

Casting doesn't round to the nearest int - it always rounds down (at least as long as only positive numbers are involved). So we get a 20% change for 0 and a 0% chance for 5 in the original question.

It is sorta covered in the JavaRanch Style Guide. |