-2

So I am new to C++, I started a few days ago. I have spent a lot of time learning it, maybe more than I should have in a few days lol. Anyways, I adopted some code that would roll a die twice and give a result. I decided to alter it some so that if the result was a prime number, you would 'win' and if it wasn't, you would lose and it would send you back through the program and you played until you got a prime number.

I decided to use what I learned from that to attempt to make a simple dice rolling program that would simply ask for the user to hit enter to run the script, and then it would roll the die for them once they hit enter. I made it so it ran through a while loop so that it would stay running so it could be used for more than one die roll.

I don't get an actual error when it prints, it all works fine except that I noticed that it will print the result, and then when I run it again it will print the same result, so if I hit enter once and get a 3, the second time I hit it I will get a 3 again, but if I hit it a third time I will get anything. I was wondering if someone would take a look at my code and maybe help me with a solution?

screenshot

It's worth mentioning that it only seems to give me the same number twice like 75% of the time, but it still seems to frequent to be a coincidence. Also, this is my first time on stackoverflow and I could not figure out how to use the code input properly so I just added an image.

Thanks for any help I get, I can add additional information if asked for. There is a change that I have some '#include' functions that I don't need, I am not entirely sure. Any tips and advice is greatly appreciated!

CiaPan
  • 9,381
  • 2
  • 21
  • 35
Cytax
  • 13
  • 1
  • Hello, and welcome to Stack Overflow. Please do not post screenshots of code, we can't compile images or scroll around in them. Post the code itself, with 4-space indent (you can do this automatically by selecting your code in Stack Overflow editor and hitting Ctrl-K or Command-K). – Amadan Jul 04 '18 at 07:11
  • Downvoted because of _NOT_ replacing a screenshot with a text source code, even after asked to. – CiaPan Jul 09 '18 at 07:45
  • Okay..? It was resolved, I don't need help, people don't need to waste their time trying to help me when it's resolved. – Cytax Jul 09 '18 at 18:13
  • @Cytax This site is not here for you only. It's for everybody. We try to make it useful not just by solving someone else's problems, but also by making these solutions useful for others, who may _search_ solutions for their similar problems. So readability and _searchability_ add usefulness. Texts in images do not. – CiaPan Jul 25 '18 at 20:53

1 Answers1

0

Don't invoke srand in a loop.

srand() initializes the random number generator. As you used time(NULL) as a parameter, it initializes the generator with the same value as long as the current second doesn't change. So all invocations of random() done within the same second, return the same result.

When the clock ticks, the generator is started again over end over with the same number, representin a new clock state, and you get a new series of equal results.

Just call srand() once, at the beginning of your main().

EDIT
See srand() — why call it only once?.

CiaPan
  • 9,381
  • 2
  • 21
  • 35
  • How would you recommend I change it then? I understand what you are saying, but I can't seem to think of an alternative to use. – Cytax Jul 04 '18 at 20:55
  • @Cytax Just remove the `srand(time(NULL));` line from `droll()` and put it as the first line in the `main()` function. – CiaPan Jul 04 '18 at 21:37
  • @Cytax Oh, and please do what Amadan said: paste your source code into the question instead of the screenshot. – CiaPan Jul 04 '18 at 21:39
  • Thank you! I was very tired when writing that post, absolutely exhausted from trying to get that code to work. In the future I will certainly do that. Thank you for your help, I appreciate it so much. – Cytax Jul 05 '18 at 01:11