0

Good afternoon. I'm working on a quiz which contains four buttons. They contain a randomly selected line of text from a text file. The problem begins when I call 4 integers with the rand() function, which picks numbers from 1-5. Knowing that the rand() function can pick the same numbers, I code this:

if (random4 == random1 || random4 == random2 || random4 == random3) {
  if (random4 == 5) {
    while (random4 == random1 && random4 == random2 && random4 == random3 ||
           random4 != 5) {
      random4--;
    }
    return Button4Making();
  } else {
    while (random4 == random1 && random4 == random2 && random4 == random3) {
      random4++;
    }
    return Button4Making();
  }
}

But now my program works fine but crashes a lot of time. I'm assuming that I need to put a limit or something like that, but I don't know how to do it.

functions :

void game_frame::Button1Making() {
  srand(time(NULL));
  int bla = 0;

  string rading;

  ifstream object(
      "C:\\Users\\Dzemail\\OneDrive\\Desktop\\database\\odgovori.dat");

  while (bla != random1 && getline(object, rading)) {
    ++bla;
  }
  if (bla == random1) {
    button1 = new wxButton(main_panel1, wxID_ANY, rading, wxPoint(10, 10),
                           wxSize(150, 50));
  } else {
    button1 = new wxButton(main_panel1, wxID_ANY, "Error", wxPoint(10, 10),
                           wxSize(150, 50));
  }
}
void game_frame::Button2Making() {
  srand(time(NULL));
  int bla = 0;
  if (random2 == random3 || random2 == random4 || random2 == random1) {
    if (random2 == 5) {
      while (random2 == random1 && random2 == random4 && random2 == random3) {
        random2--;
      }
      return Button2Making();
    } else {
      while (random2 == random1 && random2 == random4 && random2 == random3 ||
             random2 != 5) {
        random2++;
      }
      return Button2Making();
    }
  } else {
    string rading;

    ifstream object(
        "C:\\Users\\Dzemail\\OneDrive\\Desktop\\database\\odgovori.dat");

    while (bla != random2 && getline(object, rading)) {
      ++bla;
    }
    if (bla == random2) {
      button2 = new wxButton(main_panel1, wxID_ANY, rading, wxPoint(200, 10),
                             wxSize(150, 50));
    } else {
      button2 = new wxButton(main_panel1, wxID_ANY, "Error", wxPoint(200, 10),
                             wxSize(150, 50));
    }
  }
}
void game_frame::Button3Making() {
  srand(time(NULL));
  int bla = 0;

  string rading;

  ifstream object(
      "C:\\Users\\Dzemail\\OneDrive\\Desktop\\database\\odgovori.dat");

  while (bla != random3 && getline(object, rading)) {
    ++bla;
  }
  if (bla == random3) {
    button3 = new wxButton(main_panel1, wxID_ANY, rading, wxPoint(400, 10),
                           wxSize(150, 50));
  } else {
    button3 = new wxButton(main_panel1, wxID_ANY, "Error", wxPoint(400, 10),
                           wxSize(150, 50));
  }
}
void game_frame::Button4Making() {
  srand(time(NULL));
  int bla = 0;
  if (random4 == random1 || random4 == random2 || random4 == random3) {
    if (random4 == 5) {
      while (random4 == random1 && random4 == random2 && random4 == random3 ||
             random4 != 5) {
        random4--;
      }
      return Button4Making();
    } else {
      while (random4 == random1 && random4 == random2 && random4 == random3) {
        random4++;
      }
      return Button4Making();
    }

  } else {
    string rading;

    ifstream object(
        "C:\\Users\\Dzemail\\OneDrive\\Desktop\\database\\odgovori.dat");

    while (bla != random4 && getline(object, rading)) {
      ++bla;
    }
    if (bla == random4) {
      button4 = new wxButton(main_panel1, wxID_ANY, rading, wxPoint(600, 10),
                             wxSize(150, 50));
    } else {
      button4 = new wxButton(main_panel1, wxID_ANY, "Error", wxPoint(600, 10),
                             wxSize(150, 50));
    }
  }
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
Wowo312
  • 13
  • 4
  • Please post a [mcve], [you should not be using rand](https://stackoverflow.com/questions/52869166/why-is-the-use-of-rand-considered-bad), use `` library instead, it even has `std::random_shuffle` which does exactly what you want. – Quimby Feb 24 '21 at 17:06
  • Post all relevant informations in the question as text. If an image is really required use the image store provided by stackoverflow and not an external service. The provided image does not exists anymore. (Or is not reachable from my destination) – t.niese Feb 24 '21 at 17:25

1 Answers1

1

It sounds like you want a random selection of items from a list, but without repeats. An easy way to do this is to shuffle a list and then read out the items from the shuffled list. This page describes an approach for doing this: https://www.cplusplus.com/forum/general/207328/

In your case, you can create a list with the integers in order like [1, 2, 3, 4] and then shuffle the list, giving you some permutation, e.g. [2, 4, 1, 3]. This ensures you don't have repeats.

Tom
  • 18,685
  • 15
  • 71
  • 81
  • Shuffle would mean one of the numbers would never appear. – Aykhan Hagverdili Feb 24 '21 at 17:04
  • 1
    @Tom I think I will go with this solution. Thank you! – Wowo312 Feb 24 '21 at 17:34
  • @AyxanHaqverdili — shuffling just changes the order of the elements. It doesn’t remove any of them. – Pete Becker Feb 24 '21 at 17:35
  • 1
    @PeteBecker OP wanted 4 elements in the range [1-5], but this answer only generates numbers in the range [1-4]. A way to get around this would be to add all five numbers, shuffle it and take first 4. This is nice as long as the sample count you want is similar to possible range. If the range was [1-1,000,000,000] and OP wanted 4 random numbers in that range, shuffling a billion elements would be insane just to get 4 numbers. – Aykhan Hagverdili Feb 24 '21 at 18:54
  • @AyxanHaqverdili -- the question is about randomly choosing among **four** buttons. I suspect that generating [1-5] is an artifact of attempting to generate four values by brute force. – Pete Becker Feb 25 '21 at 14:05
  • @PeteBecker hmm. It's possible that I've misunderstood the question, but why would you need 4 numbers to pick 1 button? – Aykhan Hagverdili Feb 25 '21 at 14:27