Win a copy of Rust Web Development this week in the Other Languages forum!
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:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

# Random Number Genration

Greenhorn
Posts: 28
• Number of slices to send:
Optional 'thank-you' note:
Hi,
I need help in generating Random numbers between 2 integers say 1 and 20.
Also How do i ensure that it produces different values everytime i run the program.

Ranch Hand
Posts: 2545
• Number of slices to send:
Optional 'thank-you' note:
By defination, random number generator generate a number between 0 and 1 randomly, so you use
19 * random + 1
to genrator a random number between 1 and 20.

Ranch Hand
Posts: 284
• Number of slices to send:
Optional 'thank-you' note:
a series of random numbers between 1 and 20 ... do they have to be different just within the set of generated numbers? or a different set of numbers to any previously generated? or does every number have to be different every time?

Ranch Hand
Posts: 284
• Number of slices to send:
Optional 'thank-you' note:
maybe you could compare each number with the previously generated numbers using an if-else statement, and throw the number away if it is not different, but if it is, add it to the array?

Wanderer
Posts: 18671
• Number of slices to send:
Optional 'thank-you' note:
By defination, random number generator generate a number between 0 and 1 randomly, so you use
19 * random + 1
to generator a random number between 1 and 20.

Assuming you want integer values from 1 to 20 - try multiplying by 20 rather than 19. A general method is:

Math.random() uses the system time as a seed, so if you run it more than once it will not repeat the same sequence each time. You certainly may see some of the same values appear again of course. If that's not what you want, you'll need to explain more clearly.

Ranch Hand
Posts: 192
• Number of slices to send:
Optional 'thank-you' note:
private static 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 thrwoDice() is an int
//number between 1 and 7.
Is the result of "throwDice()" an int between 1 and 6 or 1 and 7?
Thomas

Ranch Hand
Posts: 284
• Number of slices to send:
Optional 'thank-you' note:
my guess is -
if the code didnt have a 1+ in front of the random number that is cast to int, the result would be between 0 and 6, but with the plus, it must be between 1 and 7?

Sheriff
Posts: 7023
• Number of slices to send:
Optional 'thank-you' note:
The answer to Thomas' question is an int between 1 and 6. Thomas, please don't post the same question in multiple forums.

 The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck. Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton