Two Laptop Bag*
The moose likes Beginning Java and the fly likes Using Methods in methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using Methods in methods" Watch "Using Methods in methods" New topic
Author

Using Methods in methods

Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Hi,

i am writing a program, and i have made a randomGenrate method, which i would like to use in my rollDice method. Any help on how i can achieve this? Thanks



Stephen Foy - Microsoft Application Development Consultant
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
What problem are you having? This should compile and run:

Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Its that i want to use the randomGenerate number in other methods, i have it working now.



I want it to print out 3 different random numbers, but it prints out the same always.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
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 (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

Joined: Oct 17, 2005
Posts: 143
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!
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8801
    
    5
Uh oh!

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

Joined: Oct 17, 2005
Posts: 143
at first i tried.

math.Random()+ 1 * 6 and never worked
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

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.


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

Joined: Oct 14, 2005
Posts: 18541
    
    8

Or you could try java.util.Random.nextInt(6) + 1; if you read its documentation, there seems to have been some thought put into making sure that nextInt() does actually return a pseudo-random sequence of integers between 0 and 5 inclusive.
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
i managed to get it to do different random values, hope this is an appropriate way. Any suggestions?

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

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


Junilu - [How to Ask Questions] [How to Answer Questions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using Methods in methods
 
Similar Threads
dice game
radiobuttons changing background color
catch statements
Help.
help on Yahtzee