-1

Salutations,

I'm writing a function that generates an array of random values between 1 and 49 for a lottery game. Assigning the random values to each slots in the array is easy enough (already wrote a functioning value generator), as is sorting the values in the array, but then I have to validate that they're all unique.

Thanks in advance for any help, either directly here or by pointing me in the good direction. (Didn't find exactly what I was looking for with the prompted suggestions.)

    void generateLottery(short lottery[], const short LENGTH, const short MIN, const short MAX) {
    for (short i = 0; i < LENGTH; i++) {
        short counter = 0;
        lottery[i] = generateRandomValue(MIN, MAX);
        while (lottery[counter] != NULL) {
            if (lottery[i] == lottery[counter]) {
                lottery[i] = generateRandomValue(MIN, MAX);
            }
            counter++;
        }
    }
    sortLottery(lottery, LENGTH);
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
tcaissie
  • 83
  • 2
  • 9
  • 8
    You need to [shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) some simply generated array with unique values. – Eugene Sh. Jul 31 '18 at 21:22
  • 1
    Take a look at [the example code for std::iota.](https://en.cppreference.com/w/cpp/algorithm/iota) – user4581301 Jul 31 '18 at 21:23
  • Does it absolutely have to be an array? Instead you could use an [unordered set](http://www.cplusplus.com/reference/unordered_set/unordered_set/) which only accepts unique values. – emsimpson92 Jul 31 '18 at 21:25
  • @emsimpson92 It does. /cry This is for an algorithm and basic programming class, and it's an exercise with arrays. – tcaissie Jul 31 '18 at 21:35
  • @EugeneSh. This does provide me wiith an actual algorithm, so I guess I'll work with that. Kind of having a brain fart at the moment, but I thank you for your assistance. – tcaissie Jul 31 '18 at 21:35
  • @user4581301 That technique looks swell, but unfortunately I can't use that as it defies the pupose of the exercise, which is to practice algorithm and, I guess, reasoning like a programmer. Nice cookie-cutter though! – tcaissie Jul 31 '18 at 21:37
  • This isn't exactly on the topic of the question, but I'd recommend revisiting the logic in your inner loop (where you check if `lottery[counter] != NULL`. `NULL` is just a macro alias of 0 with is idiomatically used exclusively with pointers (and, from C++11 onwards, is often avoided in favor of `nullptr`). In addition to idiomatic preferences, though (which ultimately aren't a strong reason to do anything anyway), there are some potential correctness concerns: what if `lottery` is passed to your function already containing non-zero values? – bionicOnion Jul 31 '18 at 21:41
  • You could throw your random number generation inside a while loop, with the condition being `find(array.begin(), array.end(), randomnum)` – emsimpson92 Jul 31 '18 at 21:42
  • That way if it finds the number in the array it just generates a new one. – emsimpson92 Jul 31 '18 at 21:43
  • 2
    Another method is to choose randomly form an array filled with the numbers 1-49, then swap the selected value with the last, then choose randomly from 1-48, and so on... – m69's been on strike for years Jul 31 '18 at 21:44
  • You can adapt that method so that you only have to store the selected numbers; then you can use it to choose e.g. numbers from 1 to a million without having to first create an array that holds a million numbers. – m69's been on strike for years Jul 31 '18 at 21:47

2 Answers2

0

Instead of

    lottery[i] = generateRandomValue(MIN, MAX);
    while (lottery[counter] != NULL) {
        if (lottery[i] == lottery[counter]) {
            lottery[i] = generateRandomValue(MIN, MAX);
        }
        counter++;

you can just use std::find to generate a new number if the number is found. Something like this...

    do
    {
      lottery[i] = generateRandomValue(MIN, MAX);
    }while(std::find(lottery.begin(), lottery.end(), lottery[i]));
    counter++;

As others have mentioned, this is not the most efficient method, so it all depends on how many entries you expect to have.

emsimpson92
  • 1,779
  • 1
  • 9
  • 24
0

Create an array of 49 integers, numbered from 1 to 49. Shuffle array. Pick first n elements. You now have n unique random numbers in the range 1,49.

Simple Shuffling:

  1. Start at index 0.
  2. Generate random number % 49.
  3. Swap arr[index] with arr[randomnumber].
  4. repeat with index 1,2,3,4...48.
MPops
  • 355
  • 3
  • 8
  • Right way to do this and discussed already in the comments. The problem is shuffle how? The usual `` tricks are denied to them. – user4581301 Jul 31 '18 at 22:00
  • @user4581301 Ah, I wasn't aware they needed help just `shuffling` the array. I was giving advice on how to have a constant time operation instead of attempting to check each time if the values are unique or not. – MPops Jul 31 '18 at 22:04