• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Using Methods in methods

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What problem are you having? This should compile and run:

 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79793
382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
at first i tried.

math.Random()+ 1 * 6 and never worked
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28304
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i managed to get it to do different random values, hope this is an appropriate way. Any suggestions?

 
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic