Forum Moderators: open

Message Too Old, No Replies

Math.Random()

how do I set a range

         

humpg

12:56 pm on May 30, 2006 (gmt 0)

10+ Year Member



After searching the internet I just can't seem to find the answer. What I am looking for is generating a random number between a certain range, say 10 to 20.

But what I keep finding is how to generate a random number between 1 and whatever. I understand how that works but how do I set the range:

example:

Math.floor((Math.random() * 9) + 1) - number between 1 and 10.

How do i change this to generate between 10 and 20 say?

fabricator

4:11 pm on May 30, 2006 (gmt 0)

10+ Year Member



Its simple maths really

Math.random() generates a number between 0 and 1. The *9 changes this to 0-9, then you need +1 to change it to 1-10.

Therefore you need to alter the +1 to change the minimum value. the *9 sets the range.

so for 20 - 30 you need
Math.floor((Math.random() * 10) + 20)