P
Print Guy
Hi all.
To most of you, what I have been working on is probably trivial and
easy, but it's taken me the good part of a week working 2 or 3 hrs a
day to finally figure out how to do what I needed to do.
Here in Canada, we have a lottery called 6-49. You pick 6 numbers
between 1 and 49. Those numbers are put on a ticket and there is a
draw twice a week.
I wanted to come up with a statistically solid way to pick my numbers
so I figured that if I were to pick 6 numbers 1,000,000 times and count
the number of times each number is selected, the top six would be good
numbers to bet on during the lottery.
With that being said, I thought I could write a Java program to do the
job. It proved to be a bit more difficult than I thought it would be;
the hardest part being ensuring that each of the 6 numbers were unique.
Here is my code. What I am hoping for is some constructive criticism
which could help me to make the code more efficient.
(Normally I would not comment my code so much, but I wanted you to see
what I was trying to accomplish)
package lotto;
import java.util.Arrays;
import java.util.Date;
import java.util.Random;
public class Sample {
public static void main(String args[]) {
Date today=new Date();
System.out.println("Starting:" + today);
// the original array of 6 numbers picked
int [] list = new int[6];
// the new array containing unique numbers only
int[] newList = new int[6];
int number = 0;
// this array is used to count the number of occurances (1-49)
int [] BigList = new int[50];
// initialize BigList
for ( int x=0;x<=BigList.length-1;x++)
BigList[x]=0;
// do this whole thing a million times
for (int y=1;y<=1000000;y++) {
// initialize newList
for (int t=0;t<=newList.length-1;t++)
newList[t]=0;
// put 6 random numbers into an array
for ( int z=0;z<6;z++) {
Random rand = new Random() ;
number=rand.nextInt(49) + 1;
list[z]=number;
}
int count=0;
// what this loop does is check each member of "list" to see
// if it's a duplicate. Copy the current element to the newList
// if it's not a duplicate.
boolean dupLocated=false;
for ( int b=0;b<=list.length-1;b++)
{
int chkNum=list;
int temp = b;
boolean found = false;
for ( int a=0;a<=newList.length-1;a++)
{
if ( newList[a] == chkNum)
{
found=true;
dupLocated=true;
}
}
if ( ! found )
newList[temp]=chkNum;
}
// now that we have at least one duplicate, we need to replace
// the zero space holder with a new random number. So
// to do that, we select a random number and then check to see
// if it is the array. If it isn't then stick in in the list
otherwise, keep
// selecting and checking until we can put it in (usually takes one or
two
// iterations
if ( dupLocated )
{
for (int x=0;x<=newList.length-1;x++)
if ( newList[x] == 0 )
{
boolean t=false;
while ( !t )
{
Random rand = new Random() ;
int number1=rand.nextInt(49) + 1;
int tt = 0;
for (int r=0;r<=newList.length-1;r++)
if ( newList[r] != 0 )
if ( newList[r] == number1 )
tt++;
if ( tt == 0 )
{
newList[x]=number1;
t=true;
}
}
}
}
// so now we have a list of 6 unique numbers. and we need to increment
// the value in BigList that corresponds to each of the six numbers.
for (int j=0;j<=list.length-1;j++)
{
int idx=list[j];
BigList[idx]++;
}
}
// now find the top six and create a new array called topSix
int [] topSix = new int[6];
for (int count=0;count<=topSix.length-1;count++)
{
int lIndex=0;
int largest=BigList[0];
// compare each element of BigList with each "other" element
// of BigList (I created this algorithm on my own, or maybe I rememered
// it from somewhere.
for (int x=0;x<=BigList.length-1;x++)
{
for ( int y=x+1;y<=BigList.length-2;y++)
if ( BigList[y] > largest )
{
largest=BigList[y];
BigList[y]=0;
lIndex=y;
topSix[count]=lIndex;
}
}
}
// now print out the list of topSix
for (int i=0;i<=topSix.length-1;i++)
System.out.println("Rank " + (i+1) + " number is " + topSix);
Date today2=new Date();
System.out.println("Ending:" + today2);
}
}
------------------------------------------------------------------------------
Sample output
Starting:Wed Aug 16 20:51:49 ADT 2006
Rank 1 number is 43
Rank 2 number is 48
Rank 3 number is 29
Rank 4 number is 30
Rank 5 number is 35
Rank 6 number is 46
Ending:Wed Aug 16 20:52:04 ADT 2006
15 seconds isn't too bad... but maybe it could be faster?
To most of you, what I have been working on is probably trivial and
easy, but it's taken me the good part of a week working 2 or 3 hrs a
day to finally figure out how to do what I needed to do.
Here in Canada, we have a lottery called 6-49. You pick 6 numbers
between 1 and 49. Those numbers are put on a ticket and there is a
draw twice a week.
I wanted to come up with a statistically solid way to pick my numbers
so I figured that if I were to pick 6 numbers 1,000,000 times and count
the number of times each number is selected, the top six would be good
numbers to bet on during the lottery.
With that being said, I thought I could write a Java program to do the
job. It proved to be a bit more difficult than I thought it would be;
the hardest part being ensuring that each of the 6 numbers were unique.
Here is my code. What I am hoping for is some constructive criticism
which could help me to make the code more efficient.
(Normally I would not comment my code so much, but I wanted you to see
what I was trying to accomplish)
package lotto;
import java.util.Arrays;
import java.util.Date;
import java.util.Random;
public class Sample {
public static void main(String args[]) {
Date today=new Date();
System.out.println("Starting:" + today);
// the original array of 6 numbers picked
int [] list = new int[6];
// the new array containing unique numbers only
int[] newList = new int[6];
int number = 0;
// this array is used to count the number of occurances (1-49)
int [] BigList = new int[50];
// initialize BigList
for ( int x=0;x<=BigList.length-1;x++)
BigList[x]=0;
// do this whole thing a million times
for (int y=1;y<=1000000;y++) {
// initialize newList
for (int t=0;t<=newList.length-1;t++)
newList[t]=0;
// put 6 random numbers into an array
for ( int z=0;z<6;z++) {
Random rand = new Random() ;
number=rand.nextInt(49) + 1;
list[z]=number;
}
int count=0;
// what this loop does is check each member of "list" to see
// if it's a duplicate. Copy the current element to the newList
// if it's not a duplicate.
boolean dupLocated=false;
for ( int b=0;b<=list.length-1;b++)
{
int chkNum=list;
int temp = b;
boolean found = false;
for ( int a=0;a<=newList.length-1;a++)
{
if ( newList[a] == chkNum)
{
found=true;
dupLocated=true;
}
}
if ( ! found )
newList[temp]=chkNum;
}
// now that we have at least one duplicate, we need to replace
// the zero space holder with a new random number. So
// to do that, we select a random number and then check to see
// if it is the array. If it isn't then stick in in the list
otherwise, keep
// selecting and checking until we can put it in (usually takes one or
two
// iterations
if ( dupLocated )
{
for (int x=0;x<=newList.length-1;x++)
if ( newList[x] == 0 )
{
boolean t=false;
while ( !t )
{
Random rand = new Random() ;
int number1=rand.nextInt(49) + 1;
int tt = 0;
for (int r=0;r<=newList.length-1;r++)
if ( newList[r] != 0 )
if ( newList[r] == number1 )
tt++;
if ( tt == 0 )
{
newList[x]=number1;
t=true;
}
}
}
}
// so now we have a list of 6 unique numbers. and we need to increment
// the value in BigList that corresponds to each of the six numbers.
for (int j=0;j<=list.length-1;j++)
{
int idx=list[j];
BigList[idx]++;
}
}
// now find the top six and create a new array called topSix
int [] topSix = new int[6];
for (int count=0;count<=topSix.length-1;count++)
{
int lIndex=0;
int largest=BigList[0];
// compare each element of BigList with each "other" element
// of BigList (I created this algorithm on my own, or maybe I rememered
// it from somewhere.
for (int x=0;x<=BigList.length-1;x++)
{
for ( int y=x+1;y<=BigList.length-2;y++)
if ( BigList[y] > largest )
{
largest=BigList[y];
BigList[y]=0;
lIndex=y;
topSix[count]=lIndex;
}
}
}
// now print out the list of topSix
for (int i=0;i<=topSix.length-1;i++)
System.out.println("Rank " + (i+1) + " number is " + topSix);
Date today2=new Date();
System.out.println("Ending:" + today2);
}
}
------------------------------------------------------------------------------
Sample output
Starting:Wed Aug 16 20:51:49 ADT 2006
Rank 1 number is 43
Rank 2 number is 48
Rank 3 number is 29
Rank 4 number is 30
Rank 5 number is 35
Rank 6 number is 46
Ending:Wed Aug 16 20:52:04 ADT 2006
15 seconds isn't too bad... but maybe it could be faster?