Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
thanks in Advance.
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic