Click to See Complete Forum and Search --> : Lottery numbers.


David Harrison
05-18-2003, 09:26 AM
I was asked by my Grandad to make a program that chooses lottery numbers. Easy I thought, no it wasn't.

The part of the script that gets the numbers is shown below and as you can see it has many loops in it. Mostly there for trying to make sure that none of the numbers are the same, however sometimes some numbers are the same which means that I've done something wrong, can anyone tell me what it is please.

while(true){escape="yes";o=0;
for(n=0;n<6;n++){nums[n]=Math.round((Math.random()*48)+1);
for(o=n-1;o>=0;o--){if(nums[n]==nums[o]){nums[n]=Math.round((Math.random()*48)+1);}}
if(nums[n]<10){nums[n]="0"+nums[n];}}
while(escape=="yes" && o<5){o++;while(escape=="yes" && n<6){n=o+1;n++;if(nums[o]==nums[n]){escape="no";}}}
if(escape=="yes"){break;}}

AdamBrill
05-18-2003, 09:56 AM
Try this instead:while(true){
escape="yes";
o=0;
for(n=0;n<6;n++){
nums[n]=Math.round((Math.random()*48)+1);
loop=true;
do{
for(o=0;o<nums.length;o++){
ran = false;
if(nums[n]==nums[o] && n!=o){
nums[n]=Math.round((Math.random()*48)+1);
ran = true;
}
}
loop = ran;
} while(loop);
if(nums[n]<10){
nums[n]="0"+nums[n];
}
}
while(escape=="yes" && o<5){
o++;
while(escape=="yes" && n<6){
n=o+1;
n++;
if(nums[o]==nums[n]){
escape="no";
}
}
}
if(escape=="yes"){
break;
}
}

David Harrison
05-18-2003, 10:42 AM
Thanks that seems to work, I haven't seen any double up's yet.