0

Even after including srand(time(NULL)) at the start of my function, (function is only called once in main) I get the same random value for r1 every time I run the program. r2 and r3 get random values fine, but I need random decimal values between 0.1 and 10.0 so what's wrong with the line containing r1?

void randNums(float &r1, float &r2, float &r3) {

   srand(time(NULL));

   r1 = (10 * (rand())/ (float)RAND_MAX);
   r2 = 1 + (rand() % 10);
   r3 = 1 + (rand() % 10);

}
Ghost
  • 249
  • 1
  • 14
  • 8
    10 * (rand()) has a 90% chance of overflowing on many systems. – Mooing Duck Jan 30 '19 at 00:20
  • 1
    `srand` resets the random number generator. `time` has a resolution of 1 second. If this function is called within the same second you will generate the same numbers. In general you only want to call `srand` once per program. In the cases where you need to call it more than once, `rand` is probably not the right tool for your job. – user4581301 Jan 30 '19 at 00:28
  • `r1 = rand() % 100 / 10.0` – 273K Jan 30 '19 at 00:29
  • I already wrote in the question that srand() was called once in the entire program – Darklord098 Jan 30 '19 at 00:30
  • I'm stupid. Reread question. Removed hold. – user4581301 Jan 30 '19 at 00:30
  • I could not reproduce: http://coliru.stacked-crooked.com/a/c4a8acd1904e7cb5 – eerorika Jan 30 '19 at 00:33
  • S.M. thank you that actually did the trick, but just out of curiosity I'd still like to know why my code wasn't working – Darklord098 Jan 30 '19 at 00:33
  • 1
    @MooingDuck has explained you why your code wasn't working. Probably you confused with parentheses. `r1 = (10 * (rand() / (float)RAND_MAX));` – 273K Jan 30 '19 at 00:37
  • @Darklord098 `rand()` generates a random number in range `[0, RAND_MAX)`, now on most implementation `RAND_MAX == INT_MAX`, this implies that if value exceeds `INT_MAX/10` then the result of multiplying by `10` causes overflow. – Jack Jan 30 '19 at 00:39
  • @S.M. i've noticed something a little weird about your solution after running the program several times. It does produce random numbers, but for r1, the random number generated is always greater than the previous number generated. After reaching a value close to 10, it goes back to 0.something the next time. This isn't happening with r2 and r3. Any reason that happens? – Darklord098 Jan 30 '19 at 00:42
  • 1
    How wedded to using `rand` are you? `uniform_int_distribution` and `uniform_real_distribution` from the [Standard Library `` library](https://en.cppreference.com/w/cpp/numeric/random) could save you some trouble. – user4581301 Jan 30 '19 at 00:54
  • 1
    Since you need 0.1 till 10.0 simply add 0.1: `r1 = 0.1 + rand() % 100 / 10.0`. – 273K Jan 30 '19 at 01:01

1 Answers1

0

I just removed the srand line and used the function to print it out a couple of times and it generated random numbers. When I was using your code to generate random numbers, it apparently displayed the same 3 random number values for r1, r2, and r3 no matter how many times I ran it. I think what Mooing Duck said was true. It might be because of overflow from 10 * rand() code. The only downside to this is, if you were to rerun your code it will generate the same random numbers as the last session.

  • yes rand() will always produce the same number of random values everytime the program is run, that's why srand(time(NULL)) is needed – Darklord098 Jan 30 '19 at 02:47