1
#ifndef REQUESTGENERATOR_H_
#define REQUESTGENERATOR_H_

#include <iomanip>
#include <iostream>

using namespace std;

class requestGenerator {
public:
int randomStar = 0;
int amountOfEvents = 0;
int randomEventArray[10];
int possibleEventArray[15] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};

void generator() {

    while (randomStar <= 2) {
        randomStar = rand() % 5;    
    }
    cout <<  randomStar << endl;

    while (amountOfEvents == 0) {
        amountOfEvents = rand() % 10;
    }
    cout << amountOfEvents << endl;



    for (int i = 0; i != amountOfEvents; i++) {
        bool numberGened = false;
        while (numberGened = false) {
            randomEventArray[i] = rand() % 15;

            if (possibleEventArray[i] != -1) {
                numberGened = true;
                possibleEventArray[i] = -1;
            }
        }
        cout << randomEventArray[i] << " ";

    }

  }
};


#endif  /*REQUESTGENERATOR_H_ */

When it outputs the generated number it gives -858993460 nine times. I am not sure why. 'possibleEventArray' is meant to hold all possible values. While the generated number goes into randomEventArray

2
  • i have the feeling that you wand to check possibleEventArray[ rand()%15 ] instead of possibleEventArray[ i]. Further, it seems like you want to generate a random permutation of the elements in possibleEventArray. For this there are duplicates around Commented Oct 30, 2018 at 13:26
  • i mean if (possibleEventArray[i] != -1) will be true always Commented Oct 30, 2018 at 13:26

1 Answer 1

3

You're never actually filling the array because of this while (numberGened = false). You're missing a second equals sign. As written, all you're doing is setting numberGened to false and not even entering the loop because numberGened = false returns the value, which is false.

Some other unsolicited comments on this:

while (randomStar <= 2) {
    randomStar = rand() % 5;    
}

If this is meant to generate a random number between 3 and 5, you can do that with randomStart = 3 + rand() % 3, as opposed to a while loop.

This for (int i = 0; i != amountOfEvents; i++) should really be i < amountOfEvents, otherwise you risk a really long loop if you modify i somewhere that goes beyond amountOfEvents.

Sign up to request clarification or add additional context in comments.

2 Comments

Its now showing real numbers, but now it is also showing the same numbers everytime. I'm using visual studio, ive cleaned the solution.
I suggest stepping through this with a debugger (or if you don't want to do that, just litter it with cout to see step by step)

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.