• Post Reply Bookmark Topic Watch Topic
  • New Topic

generating a random number in a range  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys

i know how to generate a random number from 0 to 10 using this math function



can you alter the same code to generate a number from say 10 to 30?
or is it more complicated...whats the best way?
 
Richard Hayward
Ranch Hand
Posts: 187
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted doesn't give a number from 0 to 10.

If you look at the api, 
Math.random()
gives a double from 0 up to but not including 1, so multiply by for example 11
Math.random()*11
to get a number x where
0.0 <= x <11
Then, cast to an integer
(int)(Math.random()*11)
to get an integer 0 <= x <= 10
if what you needed was say
5 <= x <= 15
then finish off by adding 5.

Adapt to suit. That's how I do it. No doubt there are other ways.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there are. I think it is far better to use a Random instance. For one thing, Math#random calculates integers behind the scenes using a Random instance already, so why do lots of arithmetic to convert it back to integers. For another, the lots of () required when you are casting to an integer are error‑prone. More discussion here.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Nice I'd like to enhance that a little:-A utility class:-
  • 1: Doesn't exist by itself; it only exists to provide services for other code.
  • 2: Contains only static members; there is therefore always one instance of the Random class in it.
  • 3: Never needs to be instantiated. See one of the few sections of the Java® Language Specification which are easy to understand.
  • I think it is a good idea for readers of the Beginning forum to create their own utility classes.
     
    Piet Souris
    Master Rancher
    Posts: 2044
    75
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Should 'max' be included as a possible outcome? If so, please check your formula.
     
    Tobias Bachert
    Ranch Hand
    Posts: 86
    18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The rand.nextInt(max - min) + min approach won't work if the result of max-min overflows. I would use either an implementation that is similar to Random#internalNextInt (-> retry until value in range)

    or, if the upper bound is exclusive:

     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12565
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    jon ninpoja wrote:hi guys

    i know how to generate a random number from 0 to 10

    I'm pretty sure that's not a random number, but a random integer.

    It's important you be precised when describing a problem, so that you get the right answers to your question.
     
    jon ninpoja
    Ranch Hand
    Posts: 291
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks for input guys

    yes i was just playing with the code before posting it...i am aware that it will produce a random integer from 0 to 50

    will try again this evening..thanks
     
    Carey Brown
    Saloon Keeper
    Posts: 3329
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tobias Bachert wrote:... or, if the upper bound is exclusive:

    Thanks for this.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!