• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Random method

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I having a bit of trouble understanding how this works

Math.random();

I know its going to spit out a number between 0 and 1 but I dont know why its in that range if there are no numbers there.

The second part I'm having trouble understanding is how to adjust it for example make it between 1 and 100
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Davey Lopez wrote:I know its going to spit out a number between 0 and 1 but I dont know why its in that range if there are no numbers there.


There are an infinite amount of numbers there. The method does not return an integer.

The second part I'm having trouble understanding is how to adjust it for example make it between 1 and 100


Think about it for a minute. You have a number between 0.0 and 1.00 (non-inclusive). How could you convert that to a larger number?
 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would assume this is the part where im supposed to multiply by 100 and add 1 or something like that?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Give it a try! (Hint: you're on the right track; but there's nuances to be accounted for).
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:There are an infinite amount of numbers there.


Well, that is not true if we talk about Java and double type. I just wanted to clarify this.

You might want to convert a number in range 0.0-1.0 to an integer but you may also get this integer directly by using java.util.Random class and its nextInt(int n) method.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would prefer to use a Random object, as PP suggests.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic