0

My code will seem amateurish as I am a software engineering student in 2nd year.

I created a lottery number generator and have noticed peculiar but consistent results. My program attempts to match the previous lottery numbers for the Euro Millions draw. I track the number of attempts it takes and i also track the number of times I match 3, 4, 5 and 6 numbers.

The attempts range between 1 million and 422 million. i.e. I would run the program 10 times and I would achieve a range, I would also track the length of time each run takes.

I account for a number of things like preventing a random number from being used more than once and this check is done against a HashMap of the possible lottery numbers. If I find the random number within the hashmap I add the number to an arraylist and then remove the number from the hashmap.

My questions surrounds the results.

In all attempts to match the lottery numbers my chance of getting 3 numbers was 3.13% on average. For 4 numbers it dropped to 0.28%, 5 numbers 0.00012% and 6 numbers 0.00022%.

Understandably The chance of winning as the number of lottery numbers increase is going to decrease however whether I had 1 million or 100 million attempts the ratio was the same or extremely close.

If you are interested my smallest number of attempts was 1,088,157, it took approximately 6 seconds or 6612ms.

Largest number of attempts was 422,036,905 and it took 26mins or 1589867ms.

Since I am using the Java Random library I am merely looking for some clarity on this. Or should I simply put it down to probability?

My code is an unnecessary 225 lines, if you would like to see a particular part or prefer to see the whole thing then please request this. Here is a sample below of the random number generation for the first 5 numbers.

//stores all possible lottery numbers
public static  HashMap<Integer,Integer> randRange = new HashMap<Integer,Integer>();

//stores bonus ball numbers
public static  HashMap<Integer,Integer> boRange = new HashMap<Integer,Integer>();

//stores lottery number output
public static  ArrayList<Integer> lotNum = new  ArrayList<Integer>();

//stores bonus ball output
public static  ArrayList<Integer> boNum = new  ArrayList<Integer>();

public static void randomInt(){

    Random rand = new Random();

    //generate a random number
    int RandInt = rand.nextInt(51);
    int boInt = rand.nextInt(12);

    //loop used to get unique random number
    int count=0;
    while(count!=5){

        //check if random number exists
        if(randRange.get(RandInt)!=null)
        {
            //finalise random number
            RandInt=randRange.get(RandInt);

            //add to ArrayList
            lotNum.add(RandInt);

            //remove number
            //ensures next random number is unique
            randRange.remove(RandInt);

            count++;                
        }

        else
        {
            //get a new random number 
            //and start process again
            RandInt = rand.nextInt(51);
        }
    }
}

EDIT:

First of all sorry I couldn't upvote as I have less than 15 reputation. All answers were helpful including comments.

Thanks to the suggestions by all members I improved my program and discovered unsurprisingly a fault in my code. @digitaljoel you were correct in the probability of matching 5 and 6 numbers. I set up the calculation incorrectly, e.g. for the numbers 11,20 30,35,45,2,3 for the euromillions draw to match 3 was 0.7%, 4 was .05%, 5 was .00273% and 6 was .000076%.

Thanks to @maybewecouldstealavan I changed my shuffling method to simply populate an ArrayList and shuffle the list, get the first five numbers and do the same for the bonus balls. The benefit was in the number of checks per second increasing from 150 - 200 thousand checks per second to 250-700 thousand checks per second.

Thanks to @trutheality as in some cases if i checked 1000 or 1,000,000 matches the variation was similar or minute.

@LeviX Appreciate again the calculation for the possible combinations. I used this within the program and found that it took more than the total number of combinations to win the lottery. Most likely I am producing duplicate random numbers. From this i will probably create all possible combinations and randomly select each combination until the program finds a match.

3
  • 3
    The only surprise I see here is that you are twice as likely to match 6 numbers as you are to match 5 numbers, whereas I would expect you to be less likely to match more numbers than less. Apr 11, 2012 at 21:51
  • What is your question exactly ? What did you expect ?
    – KeatsPeeks
    Apr 11, 2012 at 22:10
  • @Samuel_xL I was looking for an explanation as to why when using the java random library I kept achieving percentages within the same range. To be fair i should of stated this, and i guessed there would be numerous factors which could affect my results but i wasn't sure what they were.
    – jhussey
    Apr 11, 2012 at 22:43

3 Answers 3

4

In all attempts to match the lottery numbers my chance of getting 3 numbers was 3.13% on average. For 4 numbers it dropped to 0.28%, 5 numbers 0.00012% and 6 numbers 0.00022%.

Understandably The chance of winning as the number of lottery numbers increase is going to decrease however whether I had 1 million or 100 million attempts the ratio was the same or extremely close.

That is actually not surprising at all. What you end up doing here is estimating the probability of guessing 3,4,5, or 6 numbers correctly. Having more samples will only make the variations in your estimates smaller, but even with "as little" as 1 million samples, your estimate is expected to be close to the exact probability (which you could calculate by doing some math).

0

Do you mean that you expect the proportion of times that you win to be more "random"? If that's what you're getting at, then @truthreality is quite correct. For further reading you might look at the law of large numbers and the central limit theorem.

If you're asking if your method of shuffling is correct, it is though it is inefficient. You're generating more random numbers than necessary, since you're just checking for dupes when they occur, and you're not creating a new random number after you pick a ball, so you're requiring a minimum of one HashMap.get(int) per pick.

I might use one of the following methods instead:

1) Create an ArrayList containing all the ball values. For each drawing, use Collections.shuffle(yourArrList, rand) to shuffle a clone of it them, then just use the first 5 balls from the list.

2) Again, create an Array or ArrayList of ball values. Then implement a portion of the shuffle operation yourself: Choose from smaller and smaller subsets of the possibilities and swap in the element that no longer fits into the place of the element that was just chosen. The advantage is that you don't need to shuffle the entire array. Here's my quick and dirty implementation:

public static int[] choose(int[] array, int count, Random rand) {
    int[] ar = array.clone();
    int[] out = new int[count];
    int max = ar.length;
    for (int i = 0; i<count; i++) {
        int r = rand.nextInt(max); 
        //max is decremented, 
        //the selected value is copied out then overwritten 
        //by the last value, which would no longer be accessible
        max--;
        out[i]=ar[r];
        ar[r]=ar[max];
    }
    return out;
}

There's probably room for improvement, especially if order doesn't matter.

1
  • thanks for your suggestions, i know it could be more efficient and i looked at your suggested links, really helpful!
    – jhussey
    Apr 12, 2012 at 7:49
0

From my understanding there are two different parts to the Euro Millions. The 5 balls and then the 2 bonus balls. You can check the math of your program by figuring out the exact probabilities of winning. I'm sure you can google it, but it's easy to calculate.

Probability of getting 5 balls out of 50 (order doesn't matter)

P(A) = 50!/5!(50-5)! = 2,118,760

Probability of getting 2 balls out of 11 (order doesn't matter)

P(B) 11!/2!(11-2)! = 55

The two events are independent so multiply them together.

P(A) * P(B) = P(A&B)
2,118,760 * 55 = 116,531,800

Therefore the chances of winning the lottery is:

1 in 116,531,800
1
  • Thanks for the total number of unique generations, I will use this figure to implement a check i.e. if I cannot produce the winning combination within 116 million attempts then to break out of the program and return the total number of lottery numbers matched.
    – jhussey
    Apr 12, 2012 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.