-
Hi,
Now that the random function has been replaced by Math.random(), how do i render a random number from between 1 to 90? I know that Math.random returns a number greater or equal to 0 and lesser then 1. So I can actually manually multiply 100 to get 90. but what worries me is that if i multiply 100, will I be able to get 1, 2, 3 etc numbers? Does Math.random returns 0.001? What is the limit of Math.random? How many decimal places does it returns?
-
use Math.round then
Code:
Math.round(Math.random() * 90)
-
infact I should be
Code:
Math.round((Math.random() * 89) + 1)
so it goes from 1 to 90
rather than 0 to 90
-
Hmm....
If I want it to return just 1 or 2, how do i write?
-
n = (Math.random() * 2) + 1;
roundedN = Math.round(n);
-
I don't think you should use Math.round though... Math.round generates the closestes integer to the number produced by Math.random. Then there will be less chance of getting the lowest and highest values.
You should use Math.floor which will tuncate the number to the closest lower integer. So if Math.random()*10 generates 0.55 you will get 0 and not 1 and if it generates 9.99 you will get 9 and not 10. That way you will have equal chance of getting a number between 0 and 9.
So, if you want an integer between 1 and 10 you should use this code:
Code:
number = Math.floor(Math.random()*10)+1;
.b
-
bit of a change
Code:
Math.round(Math.random()) + 1
-
Math.round does round down
-
Math.round does round down
Are you sure? It does not say so in my reference... It says tha Math.floor rounds down, Math.round rounds to the closest integer and Math.xxx (seling?) rounds up...
If both Math.floor and Math.round rounds down, why are they both there? What's the difference?
.b
-
elstudion: I'm trying to get a value of either 1 or 2. If I do what you sugguested, I will end up getting 1 to 10. If I don't multiply by 10, my chances of getting 2 is very slim because 0 + 1 = 1 and the chances of getting 0 is higher then 1 if I do a round down.
I'm not too sure if Math.round does round down. but I'm sure if the decimal is below .5, it rounds down.
-
Math.floor always rounds down
Math.round rounds down if equal or less than 0.5
Math.round rounds up if more than 0.5
Math.ceil always rounds up
next time read all your documentation
try
Code:
for(i=0;i<100;i++){
intround = Math.round(Math.random());
intfloor = Math.floor(Math.random());
intceil = Math.ceil(Math.random());
trace("Math.round " + intround);
trace("Math.floor " + intfloor);
trace("Math.ceil " + intceil);
}
in this example
ceil will always return 1
floor will always return 0
and round will half the time return 1 and the other half return 0
-
i don't know if i'm thinking all wrong. But as you Mark.Broadhurst says about the functions is also what I said.
if you want a number between 0 and 4 and use Math.round isn't this the possible results?
Math.round(Math.random()*4)
0 (12,5% chance)
1, 2 and 3 (25% chance)
4 (12,5% chance)
with Math.floor it would be like this:
Math.round(Math.random()*5)
0,1,2,3 and 4 (20% chance)
which to me seems more correct... =)
???
-
Random()
Now that the random function has been replaced by Math.random(), how do i render a random number from between 1 to 90?
You can still use Random just the same - if you want a number between 1 and 90 (inclusive) then you can still use
myvariable = Random(89)+1
and there you go...
-
Originally posted by hye
elstudion: I'm trying to get a value of either 1 or 2. If I do what you sugguested, I will end up getting 1 to 10. If I don't multiply by 10, my chances of getting 2 is very slim because 0 + 1 = 1 and the chances of getting 0 is higher then 1 if I do a round down.
I'm not too sure if Math.round does round down. but I'm sure if the decimal is below .5, it rounds down.
Math.floor(Math.random()*2)+1;
Math.random()*2 will generate a number between 0 and 1.9999999 that will be floored to 0 or 1. 0 or 1 + 1 will be a number of either 1 or 2...
.b
-
Originally posted by me
....with Math.floor it would be like this:
Math.round(Math.random()*5)
...
correction: Math.floor(Math.random()*5)
Originally posted by Mark.Broadhurst
Math.floor always rounds down
Math.round rounds down if equal or less than 0.5
Math.round rounds up if more than 0.5
Math.ceil always rounds up
next time read all your documentation
try
Code:
for(i=0;i<100;i++){
intround = Math.round(Math.random());
intfloor = Math.floor(Math.random());
intceil = Math.ceil(Math.random());
trace("Math.round " + intround);
trace("Math.floor " + intfloor);
trace("Math.ceil " + intceil);
}
in this example
ceil will always return 1
floor will always return 0
and round will half the time return 1 and the other half return 0
I know that... That's why you will get the wrong results using Math.round... I hope it is you that should read your documentation ;-) (no offence)...
.b
-
actually i was typing some looong explanation to prove Math.round() was correct but when I was typing it i just noticed that you will get the same results
it's a matter of +1 at the end (with floor) or not (with random)
of course i'm not mathematical genius but that's what i noticed, hehe
--- oh actually, math.floor or ceil is only good for getting random numbers higher than 1.
but anything below that, math.round is the one to use. so i guess for consistency's sake, we should use math.round
-
end of disscusion:
http://www.elstudion.com/bobo/random.fla
this little fla has to be played through flash since it uses the trace command for all communication...
it takes a while since it uses the random function 50000 times twice.
This is what happends: using Math.round(Math.random()*5) produces an integer between 0 and 5 (that is six numbers). The trace command shows how many of each there will be in 50000 tries.
The same code is used with Math.floor(Math.random()*5). This will result in an integer between 0 and 4 (that is five numbers, the amount we are after...)
Chek the results and see for your self. When dealing with integers you have to use Math.floor to get the correct result.
.b
-
yes, just like i said, it's just a matter of putting a +1 or not
-
Code:
//get random integer of 1, 2, 3, or 4
var reps = 1000;
// Math.round()
for (i=0;i<reps;i++) {
n = Math.round(Math.random()*5);
switch (n) {
case 1:
n1++;
break;
case 2:
n2++;
break;
case 3:
n3++;
break;
case 4:
n4++;
break;
}
}
n_array = new Array();
n_array[0] = n1;
n_array[1] = n2;
n_array[2] = n3;
n_array[3] = n4;
n_array.sort(sortasc);
trace("Math.round, n1 = " + n1);
trace("Math.round, n2 = " + n2);
trace("Math.round, n3 = " + n3);
trace("Math.round, n4 = " + n4);
trace("Math.round difference = " + (n_array[3] - n_array[0]));
trace("");
n1 = 0;
n2 = 0;
n3 = 0;
n4 = 0;
// Math.floor()
for (i=0;i<reps;i++) {
n = Math.floor(Math.random()*4)+1;
switch (n) {
case 1:
n1++;
break;
case 2:
n2++;
break;
case 3:
n3++;
break;
case 4:
n4++;
break;
}
}
n_array = new Array();
n_array[0] = n1;
n_array[1] = n2;
n_array[2] = n3;
n_array[3] = n4;
n_array.sort(sortasc);
trace("Math.floor, n1 = " + n1);
trace("Math.floor, n2 = " + n2);
trace("Math.floor, n3 = " + n3);
trace("Math.floor, n4 = " + n4);
trace("Math.floor difference = " + (n_array[3] - n_array[0]));
trace("");
n1 = 0;
n2 = 0;
n3 = 0;
n4 = 0;
// Math.ceil()
for (i=0;i<reps;i++) {
n = Math.ceil(Math.random()*5)-1;
switch (n) {
case 1:
n1++;
break;
case 2:
n2++;
break;
case 3:
n3++;
break;
case 4:
n4++;
break;
}
}
n_array = new Array();
n_array[0] = n1;
n_array[1] = n2;
n_array[2] = n3;
n_array[3] = n4;
n_array.sort(sortasc);
trace("Math.ceil, n1 = " + n1);
trace("Math.ceil, n2 = " + n2);
trace("Math.ceil, n3 = " + n3);
trace("Math.ceil, n4 = " + n4);
trace("Math.ceil difference = " + (n_array[3] - n_array[0]));
trace("");
// sort function
function sortasc (a,b) {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
}
After running the above code several times with a large number for reps, there seemed to be no clear-cut winner for randomness. I used a difference between the integer that was chosen most and the integer that was chosen least to calculate randomness. Theoretically, I'm thinking, the closer to zero this difference is, the closer it is to true randomness. There may be some mathematical reason for chosing Math.round() over Math.ceil() or Math.ceil() over Math.floor(), but in the real world I don't think it's going to matter that much... Just, eh, keep your repetitions under say 10,000. :-) More than that and I will definitely refer you to a true mathemetician!
-
Are you people blind?!?! Please read this through carefully and I apologize in advance for sounding a bit angry. Don't be offended please! =)
Did you people even run my test? This is the result I get from the first run using Math.round:
0 comes up 4982 times (9.9%)
1 comes up 9961 times (19.9%)
2 comes up 10103 times (20.2%)
3 comes up 9750 times (19.5%)
4 comes up 10184 times (20.4%)
5 comes up 5020 times (10.1%)
First of all: I'm trying to get a random number of 5 possible numbers. With Math.round you get 6! In any programming language I've tried (Pascal, Visual Basic, C, C++) you have 0 as the base for integers when you do a random calculation. So if I write 5 I get a number between 0 and 4. That is 0, 1, 2, 3, 4; five numbers. If you want 1 to be the base of your calculation you add one. Just as you did with the old random function in Flash 4. And the same goes for Math.floor(Math.random()*5)+1; You will get an integer between 1 and 5 and they will all have the same chance of getting generated. As my test shows (now with the base 0 again):
0 comes up 9880 times (19.8&)
1 comes up 9956 times (19.9%)
2 comes up 10131 times (20.3%)
3 comes up 10087 times (20.2%)
4 comes up 9946 times (19.9%)
Do you understand now? 0 and 5 comes up 10% of the time when using Math.round while 1-4 comes up 20% of the time.
That is not the right way to do it. I don't care what you say, and I don't see how adding 1 could make any difference as someone said...
.b
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|