1

everybody!

I have an assignment that is to create a small program to generate different bets for the lottery. I've got hours wondering about the piece of code that should create the array with distinct numbers within each bet. The problem is that, although I've tried to cut repetitions off, my code keeps generating bets with repeating numbers within each line of the matrix. What do you think I should look more deeply?

public static void main(String[] args) {


    //Ask the user how many lines of 15 numbers will be created
    System.out.print("How many lines should the matrix have? ");
    Scanner input = new Scanner(System.in);
    int numberOfLines = input.nextInt();


    //  Create the two-dimension array.
    int[][] numbers = new int[15][numberOfLines];
    for (int i = 0; i < numberOfLines; i++) {
        for (int j = 0; j < 15; j++) {
            boolean exist = false;
            do {
                numbers[j][i] = 1 + (int)(Math.random()*25);
                for (int k = 0; k < j; k++) {
                    if (numbers[j][i] == numbers[k][i])
                        exist = true;
                    else {
                        exist = false;
                    }
                }               
            } while (exist);
        }
    }

    //Sort the array
    int temp = 0;
    for (int i = 0; i < numberOfLines; i++){
        for (int j = 0; j < 15; j++) {
            for (int k = (j + 1); k < 15; k++)
                if (numbers[k][i] > numbers[j][i]) {
                    temp = numbers[j][i];
                    numbers[j][i] = numbers[k][i];
                    numbers[k][i] =  temp;
                }
        }
    }


    //Print the array
    System.out.println();
    for(int i = 0; i < numberOfLines; i++) {
        System.out.printf("Line %d:  ", (i+1));
        for (int j = 0; j < 15; j++){
            System.out.printf("%4d",numbers[j][i]);
        }
        System.out.println();
        System.out.println();

    }
    //System.out.println(numbers[0][1]);
    //System.out.println(numbers[1][0]);
}

}

2 Answers 2

1

If your main issue is selecting a set of numbers from a larger set without duplicates, here is a simple example class that does it. Each time sample is called, the list is shuffled and the first n items are returned.

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

class Lotto {
    private List<Integer> nums = new ArrayList<Integer>();

    // constructor
    public Lotto(int m) {
        // add m numbers to list
        for (int i=1; i <= m; ++i)
            nums.add(i);
    }

    public List<Integer> sample(int n) {
        // Randomly samples n integers from 1 to m without duplicates
        Collections.shuffle(nums);
        return nums.subList(0, 6);
    }
}

Here is an example of the class being used in DrJava's interaction tab:

> Lotto from40 = new Lotto(40)
> from40.sample(6)
[37, 2, 24, 30, 19, 29]
> from40.sample(6)
[17, 8, 16, 25, 33, 39]
Sign up to request clarification or add additional context in comments.

Comments

0

What about doing something like this:

int[] createRanomArray(int size, int lowBound, int highBound) {
    Random random = new Random();

    Set<Integer> randomsSet = new HashSet<Integer>(size);

    while (randomsSet.size() < size) {
        randomsSet.add(lowBound + random.nextInt(highBound - lowBound));
    }

    List<Integer> asList = new ArrayList<Integer>(randomsSet);
    Collections.shuffle(asList);

    int[] result = new int[size];
    for (int i = 0; i < size; i++) {
        result[i] = asList.get(i);
    }
    return result;
}

now the asList list contains a random set of values, now just assign each value to a cell of your column in matrix.

A Set does not accept duplicate values, so that will assure you that no duplicate results will be there and shuffle makes items not to be in hashcode order.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.