View Full Version : creating random numbers
edmund24
02-10-2005, 08:01 AM
i hav search the forum and i notice the way of randoming the numbers is not what i want. what i need is i want to generate 10 unique random numbers from the range of 1 to 15 and put it inside an array with 10 elements inside. can someone help me!! i realli in urgent need thanks
You could do this
my_array = new array();
while (i < 9)
{
ran = random(15);
if (ran != 0){
my_array[i] = ran;
i++;
}
}
syntax isn't right I think, couldn't check it, but you get random nummers from 1 to 15 in my_array. I hope :)
cya
snapple
02-10-2005, 09:25 AM
my_array = new array();
while (i < 9)
{
ranNum = Math.floor(Math.random() * 15);
if (ranNum != 0)
{
my_array[i] = ranNum;
i++;
}
}
I think that is better syntax.
snapple
Hey,
can you please explain to me, can't see into flash right now,
Math.floor(Math.random() * 15)
Why are you using this method if you want integers and you can use random(15) for integers, don't understand right now :confused: But that's probally me :rolleyes:
cya
edmund24
02-10-2005, 12:35 PM
ok thanks guys. i got it thanks really!!
Hey,
can you please explain to me, can't see into flash right now,
Math.floor(Math.random() * 15)
Why are you using this method if you want integers and you can use random(15) for integers, don't understand right now :confused: But that's probally me :rolleyes:
cya
Since Flash 5 random() has been deprecated in favor of Math.random() which basicly means that some day random() will be dumped...
snapple
02-10-2005, 09:50 PM
ain, Why does that mean that the class method random will be dumped?
niva - thats the most correct way to produce random numbers and ensure that they are integers not floats or anything crap.
Regards, snapple :)
eey guys thanx allot!
I was thinking, could the code not be simpler like this:
my_arr = new Arrray();
for(i=0; i < 9; i++)
my_arr[i] = random(14) +1;
cheers
snapple
02-11-2005, 10:30 AM
Well, why you asking here...give it a go. Anyway, as stated above:
var myArray = new Array();
for (var i = 0; i < 9; i++ )
{
myArray[i] = Math.floor((Math.random() * 14) + 1);
trace(myArray[i]);
}
Snapple
devonair
02-11-2005, 11:09 AM
hey, edmund,
here's another way to go:
var myNumbArray:Array = new Array();
var myRandNumbArray:Array = new Array();
for (var i = 1; i < 16; i++) {
myNumbArray.push(i);
}
for (var i = 0; i < 10; i++) {
myRandNumbArray.push(myNumbArray.splice(Math.floor (Math.random() * myNumbArray.length), 1));
}
trace(myRandNumbArray);
I may have misunderstood what you were asking, but the numbers in this array will be unique as in no duplicates..
Flash Gordon
06-07-2005, 03:00 AM
Question is Math.floor(math.random()*2) really the best way to get a random number, either 0 or 1? It seems like it would come out a bit one sided. So I ran a actually test out of 52 times tested 0 came up 30. That equals 58%. 1 came up 22 out of 52 times. That equal 42%. There is a 16% difference in using this method.
Is there a better way?
Laguana
06-07-2005, 06:14 AM
Just because the amounts you got were skewed doesn't mean that it's wrong. Just means that you had random luck :P
For example, i just made a simple program to make up random numbers lots, and got this:
1: 519 0: 481 total: 1000
from this code
function myFunc() {
var num = Math.floor(Math.random() * 2)
_root.total ++
if (num>0) {
_root.ones ++
} else {
_root.zeros ++
}
trace("1: "+ _root.ones + " 0: " + _root.zeros + " total: " + _root.total)
}
setInterval(myFunc, 20)
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.