My srand don't work

Hello all,
The link below is my code this is an apple to apple with the c++ 4.22 course for Triplex section.

The problem :

  • I am using srand to generate new numbers using a seed which is the pc time but number are not
    changing with each time I run the code.

Solutions I tried :

  • Tried to google.
  • Tried adding more libraries found on google.

The result that is supposed to be :

  • whenever I rerun the program it should generate different numbers.

My main.cpp is in the link.

Thank you

So you start off with Difficulty = 1 and then run this code:

	CodeA = (rand() % Difficulty + Difficulty);
	CodeB = (rand() % Difficulty + Difficulty);
	CodeC = (rand() % Difficulty + Difficulty);

That translates to:

CodeX = 0 + 1

0 because whatever rand() returns, the remainder of a division by 1 is always 0. (When working with int). So all codes will be set to 1. You should start seeing variance once your difficulty is bigger than 1.
So, this is not an issue with rand() itself but with how you generate your codes.

Thank you tZeequemola for your reply,
I thought it was going to change from the second run, but the chances were slim on that, after doing some math to check, you are correct.
Thank you.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms