Keren Blau
Posts: 11
Nickname: kerenblau8
Registered: Jul, 2012
|
|
Re: generate a random number
|
Posted: Jul 3, 2012 6:06 AM
|
|
Hi Sarah,
There two methods I have tried:
1. In the following method the random number received is a float from 0 to, but not including, your max value. In this case you can also receive decimal number (e.g. 2.99999999999).
int min = 7; int max = 100;
Random r = new Random(); int i1 = r.nextInt(max - min + 1) + min;
2. In the following method you will receive rounded up numbers, giving you a range between 0 and yoir max value inclusive.
Math.floor(Math.random()*100); // 7 - 99 Math.floor(Math.random()*100); // 7 - 99 1 + Math.floor(Math.random()*100); // 7 - 100
randomRange = function(min, max){ return Math.floor(Math.random()*(max-min+1)) + min; } randomRange(7,100) // 7-100
Try these out, see which one works best for you.
Cheers, Keren :)
|
|