0

I received the task of simulating a lottery draw in java. The program skeleton yields the method generateOneDraw, which creates 6 random numbers between 1 and 49

static int[] generateOneDraw() {
    int numbers[] = new int[NUMBER_OF_ELEMENT_PER_DRAW];
    for(int i=0; i<numbers.length; ++i) {
        int nextNumber;
        do {
            nextNumber = generateNextRandomNumber();
        } while(numberIsInArray(nextNumber, numbers));
        numbers[i] = nextNumber;
    }
    return numbers;
}

We are then required to implement a function that simulates the lottery draw over 5 weeks and stores them in the variable draws. I believe this should be done over a two-dimensional array. Am I right in this way of thinking? Any pointers on implementing it would be greatly appreciated.

static void generateAllDraws()

Thanks in advance.

EDIT: Nevermind, I did it with a simple two dimensional array and it worked.

Nina Hain
  • 41
  • 1
  • 2
  • 8
  • I would develop a simple `Draw` class to hold the digit `List`s and have a `List` to store them. – Mena Apr 16 '15 at 11:44
  • 1
    Drawing random numbers doesn't guarantee uniqueness - this probably isn't what you want at all. You want an array with the numbers already in it, random shuffled. You current approach has the possibility of never terminating. – Boris the Spider Apr 16 '15 at 11:45
  • Thanks for the answer, but since I'm pretty new to java I think I might need some additional explanation. I'm looking to do this as simply as possible, even if it is not quite as efficient. Could you elaborate? – Nina Hain Apr 16 '15 at 11:46
  • have u considered using hashmaps? – Develop4Life Apr 16 '15 at 11:46
  • @Boris the Spider; This was the code given to me by my professor. This is not the skeleton code in its entirety, just the relevant method. – Nina Hain Apr 16 '15 at 11:46
  • @NinaHain sorry to say, but your professor is an idiot in that case. Anyone who writes `O(∞)` code shouldn't be teaching anything. – Boris the Spider Apr 16 '15 at 11:47
  • @BoristheSpider: Maybe their professor was planning to start with an example which has some pitfalls to be improved? – npinti Apr 16 '15 at 11:50
  • Since it's meant for practise, I don't really care. I am well aware that LCG generators are rather tricky, but this is not the essence of my problem. – Nina Hain Apr 16 '15 at 11:50
  • @BoristheSpider Your comment points out why big-O analysis is not the be-all and end-all. Well-constructed acceptance/rejection algorithms, while theoretically unbounded, have been known to outperform O(1) algorithms in practice because of their very low expected run-time. For an example see [Marsaglia's polar method vs Box-Muller](http://en.wikipedia.org/wiki/Box–Muller_transform) for generating Gaussians. – pjs Apr 16 '15 at 20:04

1 Answers1

1

Since this seems like home work, I will not go into much detail but you can either:

  1. Create a 2 dimensional list, as per your initial reasoning;
  2. Create a Draw class which represents a lotto draw, and create multiple instances of this class. Each Draw class could have a Date which would denote when did the draw take place.

Both approaches should work, the second approach is a little more object oriented.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • Hey @npinti! You're right, it is homework - word for word it states to "Implement the function generateAllDraws, which creates 5 draws and stores them in the static variable draws." – Nina Hain Apr 16 '15 at 11:49
  • @NinaHain: Then I would recommend you try both approaches mentioned above, or any other which other people here might propose :) – npinti Apr 16 '15 at 11:51