4

I'm trying to find the probability of next week's lotto numbers containing consecutive numbers using a Monte Carlo algorithm. I decided that sorting the numbers would probably make it easier to actually find consecutives but after a LOT of searching over the web nothing really seemed to help me for what I'm looking for

import java.util.Random;
import java.util.Arrays;
public class lotto {
    public static String getLottoTicket(){
            String lottoTicket = "";
            Random lottoNumbers = new Random();  //random object
            int [] ticket = new int[6];  //6 lotto numbers
            int counterr = 0;
            int a;
            for( int counter = 0; counter < ticket.length; ++counter ){
                ticket[counter] = (1 + lottoNumbers.nextInt(45) );  //numbers between 1-45
            }
            Arrays.sort( ticket );      //array class .. sorts array
            for(int counter = 0; counter < ticket.length; ++counter ){
                lottoTicket += ticket[counter];
                lottoTicket += " ";   
            }

            return lottoTicket;
    }
    public static void main( String [] args ){
       int a, d, n=1, t, b;
        for(int i=1; i<1000000; i++){
            int counter = 0;
            System.out.println( getLottoTicket() );
       }
    }
}

This is what I have so far and I know I'll be using a counter to find the number of consecutives over the million results but I'm literally just stumped on how to actually find consecutive numbers

genisage
  • 1,159
  • 7
  • 17
John_K
  • 114
  • 1
  • 2
  • 10
  • Arrays.sort(ticket); http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html – Ya Wang Feb 09 '15 at 20:57
  • Any consecutive numbers, as in, a single consecutive pair would count? Also, when in doubt, `return {4, 8, 15, 16, 23, 42};` – mbomb007 Feb 09 '15 at 20:57
  • 1
    Seems your lotto randomizer may return the same value more than once... (I believe that is not possible on lottery tickets) – gtgaxiola Feb 09 '15 at 20:59

2 Answers2

4

Editted answer as I read the question wrong first off, apologies!

Okay, let's break this up. So first off, how do we check if two numbers are consecutive? If they come after one another. This is correct, but how do we express this via programming?

It's actually pretty simple, two numbers are consecutive if the difference between them is equal to 1. So therefore to check if two numbers are consecutive, just subtract them and see if the result is 1 (or -1 depending on the order).

Now, the full solution to this problem is a little bit more complicated. We need to find all numbers in your generated lotto numbers that are consecutive. So, in order to break this up nicely I suggest putting this part into a separate method, called something like getConsecutiveNumbers(int [] lottoNumbers). This way, you have the basic setup done already like so:

// Now returns int array instead of String
public static int [] getLottoTicket(){
        Random lottoNumbers = new Random();  //random object
        int [] ticket = new int[6];  //6 lotto numbers
        int counterr = 0;
        int a;
        for( int counter = 0; counter < ticket.length; ++counter ){
            ticket[counter] = (1 + lottoNumbers.nextInt(45) );  //numbers between 1-45
        }
        Arrays.sort( ticket );      //array class .. sorts array

        return ticket;
}

public static void main( String [] args ){
   int a, d, n=1, t, b;
    int totalConsecutives = 0;
    for(int i=1; i<1000000; i++){
        int counter = 0;
        int [] lottoTicket = getLottoTicket();

        // Keep a count of consecutives
        totalConsecutives += getConsecutiveNumbers( lottoTicket );
   }
    System.out.println("Number of consecutives: " + totalConsecutives );
}

Now for the main part. What goes into this magic getConsecutiveNumbers() method? Let's find out.

First off let's write the signature/makeup of the method which has to be put outside of the main method, as I said it needs to take in an integer array, but also it needs to return the number of consecutive numbers, as the method name suggests, if the program asks "how many numbers are consecutive?" you should respond accordingly! The method will start off like this:

public static int getConsecutiveNumbers(int [] lottoNumbers) {

    // How many

}

Okay. Because of the way your program is setup so far we can assume that the lotto numbers given to us here are sorted already. This is good, it means we just have to go through the array and check one-by-one if the numbers are consecutive. How will we do this you say? Let's break this up also.

We need to go through the array to check each element, so that will definitely require some sort of loop. I see you've already used a for loop so let's go for that.

for(int i = 0; i < lottoNumbers.length; i++) {
    // Going through each number here
}

Now here's the intelligent part. For each iteration of this loop we need to check if element lottoNumbers[i] is consecutive to lottoNumbers[i-1]. You see? In english this could mean the 2nd element is consecutive to the 1st element and then the 3rd element is consecutive to the 2nd element and so on until i reaches the end of the array.

BUT there's one problem. At the start of this loop i is zero (which is what we want), however in this case, lottoNumbers[i-1] will give an IndexOutOfBoundsException, an array has no element at index -1! So to avoid this problem we need to start the search at one instead of zero.

public static int getConsecutiveNumbers(int [] lottoNumbers) {
    // i starts at 1 not 0
    for(int i = 1; i < lottoNumbers.length; i++) {
        // Going through each number here
    }

}

So, how do we finish this off? Let's think of it this way. While we're going through the array, every time a number is consecutive, just add it to a counter and at the end, return this counter.

Here's the final solution:

public static int getConsecutiveNumbers(int [] lottoNumbers) {
    int consecutives = 0;
    // i starts at 1 not 0
    for(int i = 1; i < lottoNumbers.length; i++) {
        // Going through each number here
        if(lottoNumbers[i] - lottoNumbers[i-1] == 1)
            consecutives++; // Numbers were consecutive

    }

    // Return the number of consecutives
    return consecutives;
}

I hope this helps you understand the problem and benefits you more than just a straight up answer! Any questions please ask away :)

Rob
  • 305
  • 1
  • 8
  • Thanks very much.. I completly understand this thanks to your examples/explanations ! – John_K Feb 09 '15 at 21:53
  • Sorry for the mountain of text. Glad to help! :) – Rob Feb 09 '15 at 21:57
  • One thing .. When I'm trying to compile it's saying that it can't find the 'ticket' variable (main method) just wondering if that's the right variable and I have to tinker the code around a bit or if it;s the wrong variable? – John_K Feb 09 '15 at 22:20
  • Sorry I made a mistake! Updating now. (I haven't tried to compile any of this so any more errors just let me know) – Rob Feb 09 '15 at 22:22
0

I guess you need to count the consecutive numbers in each ticket. Given that your array is sorted, you just need add 1 to a counter each time you find two numbers which the difference is 1, like this:

long countConsecutives(int[] sortedNumbers) {
    int nConsecutives = 0;
    for (int i = 0; i < sortedNumbers.length - 1; i++) {
        if (sortedNumbers[i + 1] - sortedNumbers[i] == 1) {
            nConsecutives++;
        }
    }
    return nConsecutives;
}

Note that if you have six numbers in your ticket, like [1, 2, 3, 4, 5, 6], this will return 5, since it counts the difference between pairs, and you have 5 consecutive pairs: (2-1), (3-2), (4-3), (5-4), and (6-5).

Also note that by generating your ticket by adding random numbers to an array you are prone to get duplicated numbers. To understand how to create an array with n different random numbers, please read this answer: Select a random int that doesn't exist

Community
  • 1
  • 1
Anderson Vieira
  • 8,919
  • 2
  • 37
  • 48