This content has been marked as final.
Show 12 replies

1. Re: random without 0
NSurveyor Jun 19, 2006 5:26 PM (in response to hcinderela)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
Newsgroup_User Jun 19, 2006 5:26 PM (in response to hcinderela)Doh ... You add one!!
eg. Math.floor(Math.random()*10)+1;

Jeckyl

3. Re: random without 0
NSurveyor Jun 19, 2006 5:39 PM (in response to hcinderela)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
Newsgroup_User Jun 19, 2006 6:19 PM (in response to hcinderela)> 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
NSurveyor Jun 19, 2006 7:41 PM (in response to Newsgroup_User)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 pseudorandom number n, where 0 <= n < 1" 
6. Re: random without 0
Newsgroup_User Jun 20, 2006 12:29 AM (in response to hcinderela)Hi!
// returns a random number between (and including) a and b
getRandom = function(a,b) {
return Math.round(Math.random()*(ba))+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
Newsgroup_User Jun 20, 2006 12:51 AM (in response to hcinderela)> return Math.round(Math.random()*(ba))+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()*(ba+1))+a;

Jeckyl

8. Re: random without 0 math.round solution no good
Newsgroup_User Jun 20, 2006 1:09 AM (in response to hcinderela)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()*(ba))+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()*(ba))+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()*(ba+1))+a;
> 
> Jeckyl
>
>

9. Re: random without 0 math.round solution no good
NSurveyor Jun 20, 2006 4:22 AM (in response to Newsgroup_User)Jeckyl is right. Theoretically, you will only get a, when Math.round(Math.random()*(ba)) is equal to 0. This is only true, when 0<=Math.random()*(ba) < 0.5. For the next value, a+1, Math.round(Math.random()*(ba)) must equal 1. This occurs only when 0.5<=Math.random()*(ba)<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()*(ba)) is equal to ba. This is only true when ba0.5<=Math.random()*(ba)<ba. A and B is half as likely as all the other numbers. 
10. Re: random without 0 math.round solution no good
Newsgroup_User Jun 20, 2006 4:46 AM (in response to hcinderela)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()*(ba))+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
Newsgroup_User Jun 20, 2006 4:50 AM (in response to hcinderela)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
Newsgroup_User Jun 20, 2006 4:54 AM (in response to hcinderela)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()*(ba))+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
>
>