Click to See Complete Forum and Search --> : Creating random lottery numbers


clairec666
11-11-2003, 06:25 AM
I have found scripts that select 6 random numbers, like a random lottery, but how can I let the user select how many numbers they want to pick?

The scripts I have found already look something like this:

var pick = new Array(6);
for (i=0; i<6; i++) {
pick[i] == "":
}
while(pick[5] == "") {
var j = Math.ceil(Math.random()*49);
if(pick[0]=="") pick[0]=j;
else if(pick[1]==""&&j!=pick[0]) pick[1]=j;
else if(pick[2]==""&&j!=pick[1]&&j!=pick[0]) pick[2]=j;

etc., where I have to compare the random number to all the already selected numbers each time to see if it has already been chosen.

How would I do this with an undefined number of numbers to be picked?

Khalid Ali
11-11-2003, 06:31 AM
take a look at this line in the code

while(pick[5] == "")

it means that run the loop until the 6th element in array is empty,

you can set this number as higher as you want..achieve what you intended.

clairec666
11-11-2003, 06:38 AM
Yeah, I know how to change that bit.....
eg. var select = any number;
while(pick[select] == "") {

but how would I change the bit after that?
I don't know if thtis makes any sense:
At the moment, with the 6-number version, I have to check the new random number against all the numbers that have already been picked, eg, for pick[5] I have to write out the code:
if(pick[5]==""&&j!=pick[4]&&j!=pick[3]&&j!=pick[2]&&j!=pick[1]&&j!=pick[0]) pick[5] = j;

Is there another way to check it againt all the previously chosen numbers? I can't carry on with the way above because it can't deal with an infinitely long array length