• Post Reply Bookmark Topic Watch Topic
  • New Topic

give me a number between 100-200  RSS feed

 
Evan Pierce
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to get a random number from between 100 and 200, what is the best way of doing this?
 
Matt Harrah
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
99 + (int) Math.ceil(Math.random() * 101)
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively you could use:
Especially if you are going to reuse the random number generator i.e. you are generating more than one random number.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want the possibility of 200 being shown? Matt Harrah's method might show 200; I think Garrett Rowe's might only show up to 199.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Do you want the possibility of 200 being shown? Matt Harrah's method might show 200; I think Garrett Rowe's might only show up to 199.
You're right... those dang off by one errors always kill me.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually a pedantic mathematician would say the correct range to show is 101-200.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!