Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW DOES ONE USE THE SRAND AND RAND() FUNCTIONS

Status
Not open for further replies.

ankoump

Programmer
May 29, 2002
11
0
0
DE
I am trying to write a function which selects a value say between 2 and 10 randomly after each run but i cannot understand how to use the Srand function and rand() function.Please Help
 
using rand:
Do this Once only:
LARGE_INTEGER gint;
QueryPerformanceCounter(&gint); //
srand(gint.LowPart);

Do this every time you need a random number 2..10
int p = (rand() % 9) + 2 ;
 
you can also try this:

#include<stdilb.h>
#include<time.h>
....
..
int number;
srand((unsigned)time( NULL ));
number = ( rand() % 9 ) + 2;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top