mardi 13 février 2018

C++: fill array with random numbers [duplicate]

This question already has an answer here:

I cannot find the answer to this for C++. What I am trying to do is to create an array of 5 values and make all 5 values random between 0 and 15.

This is what I have so far:

int *PickingNumbers(int _pickedNumbers[]){
    int pickedNumbers[5];
            for (int nr = 0; nr <5; nr++){
                srand(time(NULL));
                pickedNumbers[nr] = 1 + (rand() % 15);
                cout << 1 + nr << ": " << pickedNumbers[nr] << "\n";

        }
            return pickedNumbers;
    }

    int main(){
    int _pickedNumbers[5];
      int *_pickedNumbersResult;
      _pickedNumbersResult = PickingNumbers(_pickedNumbers);


    }

I am new to coding and just messing around with the stuff I learned, but I cant get 5 random numbers. I keep getting 5 times the same number. Which number it is does seem to be random, but they're all the same number :(

What am I overlooking in my code? Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire