1 Replies - 133 Views - Last Post: 11 September 2012 - 08:09 PM Rate Topic: -----

#1 wc1027  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 29-March 11

random number problem.

Posted 11 September 2012 - 07:47 PM

I have code of:
	srand ( time(NULL) );
	j = rand() % 10 + 1;	
	while (num<=j)
	{
		srand ( time(NULL) );
		r = rand() % 3 + 1;
		switch (r)
                {case 0:{c = 'a';}break;
                 case 1:{c = 'b';}break;
                 case 2:{c = 'c';}break;
                 default: {cout << "error!!!!";}break; 
        }                  



Why would the random int set to r be the same for every instand of the while loop?
meaning every time i run it regardless how many times it runs through the loop every char c is the same for all.

Is This A Good Question/Topic? 0
  • +

Replies To: random number problem.

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2485
  • View blog
  • Posts: 8,521
  • Joined: 08-August 08

Re: random number problem.

Posted 11 September 2012 - 08:09 PM

You're calling srand in the while loop. You should only call it once at the beginning of the code.

This post has been edited by CTphpnwb: 11 September 2012 - 08:10 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1