# Using Methods in methods

Stephen Foy

Ranch Hand

Posts: 143

Stephen Huey

Ranch Hand

Posts: 618

Stephen Foy

Ranch Hand

Posts: 143

Campbell Ritchie

Sheriff

Posts: 48940

60

posted 10 years ago

You need to check the Math.random() method carefully in the API. If I remember correctly, it returns a double number pseudo-randomly chosen between 0 and under 1, or something like 0 . . . 0.999999999....

Firstly, you should try to avoid using more than one random number generator within an app; if you hve two which are set off simultaneously, they may produce the same number each.

Secondly, if you multiply by 6, you will never get a "6;" you will get from 0 to 5.999999..., so you have to add 1 to your

The reason you are getting the same number printed out three times is that you have no means of changing the number in your loop in your rollOne() method.

Another way to get pseudo-random numbers would be the nextInt(6) method in the java.util.Random class. Again you would have to add 1.

So, a much simpler way to sort out the problem is

When I first encountered the random number generating methods, I wrote out a class which mimics rolling a die, so I can use it whenever I want random numbers in a particular tange.

BTW: There is an example of random number generation in Deitel's book, where they add all the 1s, the 2s, the 3s etc, to count how many times each numebr comes up in, say, 10000 rolls, and they also count how many times the same number comes up twice in succession; if you have a die and roll it, you should get the same number twice once in 6 pairs, the same number thrice once in 36, etc etc.

Firstly, you should try to avoid using more than one random number generator within an app; if you hve two which are set off simultaneously, they may produce the same number each.

Secondly, if you multiply by 6, you will never get a "6;" you will get from 0 to 5.999999..., so you have to add 1 to your

*(int)(randomNumber * 6)*. Using round will actually bias your numbers; you will occasionally get a 6 or a 0, but with a lower probability than for 1 2 3 4 and 5.The reason you are getting the same number printed out three times is that you have no means of changing the number in your loop in your rollOne() method.

Another way to get pseudo-random numbers would be the nextInt(6) method in the java.util.Random class. Again you would have to add 1.

So, a much simpler way to sort out the problem is

**EITHER****OR**When I first encountered the random number generating methods, I wrote out a class which mimics rolling a die, so I can use it whenever I want random numbers in a particular tange.

BTW: There is an example of random number generation in Deitel's book, where they add all the 1s, the 2s, the 3s etc, to count how many times each numebr comes up in, say, 10000 rolls, and they also count how many times the same number comes up twice in succession; if you have a die and roll it, you should get the same number twice once in 6 pairs, the same number thrice once in 36, etc etc.

Stephen Foy

Ranch Hand

Posts: 143

posted 10 years ago

Thanks for that, really helps. Oh and i managed to sort the getting a 0 problem, by using a while loop, which looped the number till it wasnt 0.

Thanks alot!

Thanks alot!

Stephen Foy - Microsoft Application Development Consultant

Bert Bates

author

Sheriff

Sheriff

Posts: 8898

5

posted 10 years ago

Uh oh!

That sounds like an approach that could lead to severly un-random dice!

I think the (Math.random() * 6) + 1 idea is better

Oh and i managed to sort the getting a 0 problem, by using a while loop, which looped the number till it wasnt 0.

That sounds like an approach that could lead to severly un-random dice!

I think the (Math.random() * 6) + 1 idea is better

Spot false dilemmas now, ask me how!

(If you're not on the edge, you're taking up too much room.)

Stephen Foy

Ranch Hand

Posts: 143

posted 10 years ago

multiplying by 6, adding 1 and rounding down should always give you a value of 1,2,3,4,5 or 6.

if you get the lowest possible value from the RNG, 0, add 1, you get 1, rounded down is 1.

if you get the highest possible value, 5.9999... to however many decimal places, add one, you get 6.99999...., and rounded down give you 6.

if there is any difference in the distribution, this is the first i've ever heard of it.

if you get the lowest possible value from the RNG, 0, add 1, you get 1, rounded down is 1.

if you get the highest possible value, 5.9999... to however many decimal places, add one, you get 6.99999...., and rounded down give you 6.

if there is any difference in the distribution, this is the first i've ever heard of it.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Stephen Foy

Ranch Hand

Posts: 143

posted 10 years ago

You might find some useful information on random numbers here:

http://www.javaranch.com/maha/Resources/gotchas_1_.html

http://javaa.com/modules.php?name=News&pagenum=29

http://www.javaranch.com/maha/Resources/gotchas_1_.html

http://javaa.com/modules.php?name=News&pagenum=29

Junilu - [How to Ask Questions] [How to Answer Questions]