12 Replies Latest reply on Jun 20, 2006 4:54 AM by Newsgroup_User

# random without 0

I am constructing a simple guess the number game. I need the computer to pick a number between 1 and 10. Currently I am using random (11) but that still includes 0. How can I exclude it?
• ###### 1. Re: random without 0
random(10) gives 0 through 9.... which means
random(10)+1 gives 1 through 10! :)

Also random is deprecated.. it stills works, but it's better to use:

Math.ceil(Math.random()*11)

Math.random() gives you a random decimal between 0 and 1 (exclusive).
Math.random()*11 gives you a random decimal between 0 and 11 (exclusive).
If we round this value up, we should get an integer ranging from 1 to 11.
• ###### 2. Re: random without 0
Doh ... You add one!!

eg. Math.floor(Math.random()*10)+1;
--
Jeckyl

• ###### 3. Re: random without 0
Whoops... I messed up the second half of my post. It would be:

Math.ceil(Math.random()*10)

Jeckyl's way works, too.
• ###### 4. Re: random without 0 - Math.ceil does NOT work
> Math.ceil(Math.random()*10)

No .. Math.ceil won't work .. because Math.ceil(0) is 0 .. so you could
still get a zero value.(and consequently a slightly smaller change of
getting 10 compared to other numbers)

You would need to use instead the method I posted before.
--
jeckyl

• ###### 5. Re: random without 0 - Math.ceil does NOT work
My bad. I always thought 0<Math.random()<1, but now that I take a close look in the Help Docs, I see that it "Returns a pseudo-random number n, where 0 <= n < 1"
• ###### 6. Re: random without 0
Hi!

// returns a random number between (and including) a and b
getRandom = function(a,b) {
return Math.round(Math.random()*(b-a))+a;
}

var myNumber = getRandom(1,10);

/Jensen/

"hcinderela" <webforumsuser@macromedia.com> wrote in message
news:e77cjp\$b3p\$1@forums.macromedia.com...
>I am constructing a simple guess the number game. I need the computer to
>pick a number between 1 and 10. Currently I am using random (11) but that
>still includes 0. How can I exclude it?

• ###### 7. Re: random without 0- math.round solution no good
> return Math.round(Math.random()*(b-a))+a;

That's just as bad .. it doesn't return even distribution .. the values for
a and b are half as likely than other values

So best is still

Math.floor(Math.random()*10)+1;

or if you want whole number between a and b inclusive

Math.floor(Math.random()*(b-a+1))+a;
--
Jeckyl

• ###### 8. Re: random without 0- math.round solution no good
Well, I ran some tests which proves your statement wrong based on empirical
results. Code code included below.
I got the following results:
repeatNr = 1000000:
undefined,111304,111303,111305,111303,111302,111307,111303,111302,111300

repeatNr = 100000:
undefined,11187,11191,11193,11189,11190,11192,11192,11192,11191

repeatNr = 10000:
undefined,1091,1092,1093,1095,1095,1094,1090,1091,1092

repeatNr = 1000:
undefined,99,99,98,92,100,100,100,99,101

repeatNr = 100:
undefined,12,7,9,10,9,8,9,11,9

// My modified code:
// returns a random number between (and including) a and b
getRandom = function(a,b) {
return Math.round(Math.random()*(b-a))+a;
}

var repeatNr = 1000000;
var L:Array = new Array();
for (var i = 1; i < 10; i++) {
L = 0;
}

for (var i = 0; i < repeatNr; i++) {
L[getRandom(1,9)]++;
}

trace(L);

"Jeckyl" <jeckyl@hyde.com> wrote in message
news:e789e9\$bda\$1@forums.macromedia.com...
>> return Math.round(Math.random()*(b-a))+a;
>
> That's just as bad .. it doesn't return even distribution .. the values
> for a and b are half as likely than other values
>
> So best is still
>
> Math.floor(Math.random()*10)+1;
>
> or if you want whole number between a and b inclusive
>
> Math.floor(Math.random()*(b-a+1))+a;
> --
> Jeckyl
>
>

• ###### 9. Re: random without 0- math.round solution no good
Jeckyl is right. Theoretically, you will only get a, when Math.round(Math.random()*(b-a)) is equal to 0. This is only true, when 0<=Math.random()*(b-a) < 0.5. For the next value, a+1, Math.round(Math.random()*(b-a)) must equal 1. This occurs only when 0.5<=Math.random()*(b-a)<1.5 (notice the broader range than a's). This continues for all values up to b. However, at b...

You will only get b, when Math.round(Math.random()*(b-a)) is equal to b-a. This is only true when b-a-0.5<=Math.random()*(b-a)<b-a. A and B is half as likely as all the other numbers.
• ###### 10. Re: random without 0- math.round solution no good
You've got faulty code to test a faulty function, so your results are
incorrect.

Your test script is faulty because of this line...

> L[getRandom(1,9)]++;

This actually calls the random number function twice .. once to get the
value and once to store the answer back again. ie the code as compiled is
the same as

> L[getRandom(1,9)] = L[getRandom(1,9)]+1;

If you correct your test code, then you get the correct sort of results...

eg

getRandom = function(a,b) {
return Math.round(Math.random()*(b-a))+a;
}
var repeatNr = 10000;
var L = new Array();
for (var i = 1; i < 10; i++) {
L = 0;
}
for (var i = 0; i < repeatNr; i++) {
var r = getRandom(1,9);
L[r]++;
}
for (var i = 1; i < 10; i++) {
trace(L
);
}

Give you the results:

616
1297
1244
1235
1271
1212
1205
1292
628
--
Jeckyl

• ###### 11. Re: random without 0- math.round solution no good
As further evidence that your results are faulty .. look at this:

repeatNr = 100:
undefined,12,7,9,10,9,8,9,11,9

The sum of all the values should add to give 100 is you got it right ..
instead you get 84
--
Jeckyl

• ###### 12. Re: random without 0- math.round solution no good
Hmm ... interesting. I agree with the logic, I just couldn't support it with
the test ... I see now why. Thanks :-)

"Jeckyl" <jeckyl@hyde.com> wrote in message
news:e78n7e\$sro\$1@forums.macromedia.com...
> You've got faulty code to test a faulty function, so your results are
> incorrect.
>
> Your test script is faulty because of this line...
>
>> L[getRandom(1,9)]++;
>
> This actually calls the random number function twice .. once to get the
> value and once to store the answer back again. ie the code as compiled is
> the same as
>
>> L[getRandom(1,9)] = L[getRandom(1,9)]+1;
>
> If you correct your test code, then you get the correct sort of results...
>
> eg
>
> getRandom = function(a,b) {
> return Math.round(Math.random()*(b-a))+a;
> }
> var repeatNr = 10000;
> var L = new Array();
> for (var i = 1; i < 10; i++) {
> L = 0;
> }
> for (var i = 0; i < repeatNr; i++) {
> var r = getRandom(1,9);
> L[r]++;
> }
> for (var i = 1; i < 10; i++) {
> trace(L
);
> }
>
> Give you the results:
>
> 616
> 1297
> 1244
> 1235
> 1271
> 1212
> 1205
> 1292
> 628
> --
> Jeckyl
>
>