Public Group

# Srand()

This topic is 4348 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This prog randomizes 4 selections Example: Clover <----Never changes every time i run the prog.. Spade Diamond Heart Clover <----Never changes every time i run the prog.. Diamond Spade Clover Clover <----Never changes every time i run the prog.. Heart Heart Spade Well it randomizes the last 3 selections, but everytime i run the program the first selection is Clover! Why is not seeding the first random generated Suit in this case?
void RangedRandSuit( int range_min, int range_max, int n )
{

srand( (unsigned)time( NULL ) );

int i;

for ( i = 0; i < n; i++ )
{

int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)
+ range_min;

switch (u)
{
case 1:
printf("%s\n",SuitDisp1);
break;
case 2:
printf("%s\n",SuitDisp2);
break;
case 3:
printf("%s\n",SuitDisp3);
break;
case 4:
printf("%s\n",SuitDisp4);
break;

}

//printf ("%d \n", u);
}

}


##### Share on other sites
Try to only call srand() once in the entire program. I usually just call it right after I drop into main or WinMain. I would assume your program is running so fast that the same value is getting passed into srand(), but that wouldn't explain why the other 3 cards are random.

G'luck

Edit: Oh yeah, your time function returns a number of seconds. You should probably use something that has millisecond precision to seed your rand. Include <windows.h> and use the GetTickCount() function. That is one of my favorites. Althought, I hope you understand how fast your cpu runs that code :) I didn't for a long time... I think your cpu could process that function something like 20 million times a second. So ms precision still isn't going to save your srand :( I Hope it helps to only call it once.

##### Share on other sites
Thanks for the tip, im just barely becoming a programmer I THINK!?

Check this out, i didnt know where to put your GetTickCount() at but something told me to replace the

srand((unsigned)time(null))
with
srand((unsigned)GetTickCount())

and it works perfectly, i hope i dont look like a total noob. lol

thanks a lot..

##### Share on other sites
Damn it, this is the beginner forum. I don't get to flame.
Haha, jk.

I suggest you bookmark
http://search.msdn.microsoft.com/search/

When you are programming, keep this web page opened at all times. Anytime you don't understand a function, variable type, anything: type it in and hit search. You should probably type in "c++", hit search, then bookmark the page so the search box has that there already.
The format is incredible for a quick reference. Usually you will search for a function, then it will tell you the parameters you need to call it, followed by the return value, and then any special remarks and usually code samples.
Oh and my favorite part, is at the very bottom. It has links to similar functions and data types and stuff, it really helps you navigate to stuff that you'll need.
And I can't stress this enough... if you really want to be a good programmer, you are going to need to commit every word of the entire msdn to memory. You might think I'm kidding. Its almost sad, but every single sentence in the MSDN is important. Well, I suppose you don't have to commit it to memory if you have the page open.
g'luck

##### Share on other sites
Yes i know this and i apologize for being noobish, i do keep the msdn library open, but as to this situation i didnt know of gettickcount, and i shouldve went and looked at the syntax in the library, but wouldnt you know i didnt need too. thx though

##### Share on other sites
Just to make sure you're clear on this: you don't need GetTickCount (though it does no harm), you just need to make sure you only call srand once per program. Passing time(NULL) to it will work fine.

##### Share on other sites
actually it doesnt, maybe im doing something wrong, but like i said, changing to gettickcount fixed evrything..

##### Share on other sites
What he's saying is that time(NULL) works if you remove srand() from RangedRandSuit() and place it at the beginning of main(). You should do this to avoid potential problems later even though your code works now.

• 10
• 18
• 14
• 18
• 15