#rand() giving same number every time

7 messages · Page 1 of 1 (latest)

sturdy glacier
#

every time I run the following code:

#include <ctime>

int rolldie(){
  srand(time(NULL));
  int x;
  return random()%6;
}

int calcplayersroll(int roundnumber){
  int running_score = 0;
  int die = rolldie();
  int die2 = rolldie();
  int die3 = rolldie();
  std::cout << die << "\n";
  std::cout << die2 << "\n";
  std:: cout << die3 << "\n";```
it gives the same number all three times, different each time I run the code
does anyone know how to make it give different numbers each time
tame groveBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

blissful igloo
#

The iterations happen too quickly for the time to change to the next value. Use srand() only once at the beginning instead of doing it on each iteration.

sturdy glacier
#

thanks

#

its working now, thank you

#

!solved

tame groveBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity