0

I want to generate a random number every time the program is run. I will output it to a file using ofstream. However, I have checked the file and the same number is returned. Does anyone know why my code below does not generate a random number within the bounds everything it is run? I think it static but not sure why.

int tempJ = rand() % 10000;
AppleSh
  • 21
  • 1
  • you need to seed first using [`srand()`](http://en.cppreference.com/w/cpp/numeric/random/srand) – Biruk Abebe May 12 '16 at 14:48
  • do you use `srand()` to seed the rng? – 463035818_is_not_an_ai May 12 '16 at 14:49
  • btw getting the same number every time is just as random as getting a different number every time, but you are probably right, that it actually isnt random – 463035818_is_not_an_ai May 12 '16 at 14:50
  • 2
    @tobi303 [It can happen](http://2we26u4fam7n16rz3a44uhbe1bq2.wpengine.netdna-cdn.com/wp-content/uploads/121411_1611_SecureRando1.png) – NathanOliver May 12 '16 at 14:51
  • humans tend to see patterns everywhere, but the sequence 9 9 9 9 9 9 9 9 9 is just as likely to appear as 3 2 5 9 1 6 3 4 7. Anyhow if you want random numbers you should use `` and not `rand()`. – 463035818_is_not_an_ai May 12 '16 at 14:53
  • 1
    Try using the C++ Standard Library pseudorandom number library. Ther'es a good example on this page: http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution – Fred Larson May 12 '16 at 14:53
  • As a side note: The reason it generates the same sequence each time is so that you can test your code. A random sequence that changes is hard to use in your unit tests. – Martin York May 12 '16 at 15:12

0 Answers0