0

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!

EDIT: answered in comments by @StoryTeller.

Xavier93
  • 13
  • 7
  • 3
    You aren't supposed to seed the random number generator every iteration. You are meant to do it only *once*. Try doing it early in main. – StoryTeller - Unslander Monica Feb 13 '18 at 15:51
  • If you're using at least C++11 you should use something better than `rand()` try `std::random_device rd; std::mt19937 mt(rd());` etc. – Justin Randall Feb 13 '18 at 15:54
  • @JustinRandall - There's nothing "better" about it for a beginner. It's enough of a hassle to figure out `rand`. – StoryTeller - Unslander Monica Feb 13 '18 at 15:54
  • @StoryTeller - thanks, that was the solution! – Xavier93 Feb 13 '18 at 16:03
  • @JustinRandall - thank you for your input but as StoryTeller said it is already hard enough to figure out rand(). I bet your suggestion will come later on in my course! – Xavier93 Feb 13 '18 at 16:05
  • srand/rand is fine (well, you don't want to use that as a source of randomness for cryptographic algorithms, but I doubt that's what you're doing, and random_device seeding/mt19937 isn't fine either in that regard) but to elaborate on @JustinRandall here's a little snippet with comments : https://ideone.com/D1SO6N – Caninonos Feb 13 '18 at 16:05
  • 2
    @Caninonos You're not seeding the RNG correctly in your example code. See [here](http://www.pcg-random.org/posts/cpp-seeding-surprises.html), [here](https://kristerw.blogspot.co.uk/2017/05/seeding-stdmt19937-random-number-engine.html) or [here](https://codereview.stackexchange.com/questions/109260/seed-stdmt19937-from-stdrandom-device). – Simple Feb 13 '18 at 16:32
  • @Simple Well, it depends on what you mean by "correctly" : this compiles, and this gives me a sequence of pseudo random numbers which are enough to toy a bit. Although, yes, using a single integer with at most 2^32 different values is a bad way to seed a pseudo random generator with a period of 2^19937-1. Admittedly, mt19937 turns out to be overkill and a bit useless with such a small seed and I should have said that seeding this way isn't very good (rather than saying using random_device in general isn't very good), but I only wanted to elaborate a bit on Justin Randall's comment. – Caninonos Feb 13 '18 at 16:47
  • @Simple still a valid and useful remark though. – Caninonos Feb 13 '18 at 16:50

0 Answers0