• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random method  RSS feed

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would prefer to use a Random object, as PP suggests.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!