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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Number Output

Status
Not open for further replies.

BrianCPP

Technical User
Sep 20, 2002
3
US
Hello, I am writing a program that has to sort random numbers from a file. I need to write these, say 50 randomly generated numbers to a file so that I can test my sorting alogorithms to see the difference in the time it takes for each sort to process. The random numbers have to be the same every time so the times for the sorts are accurate. What is the code to write the randomly generated numbers to the file. Any help would be appreciated. Thanks BrianCPP
 
#include <time.h>
#include <stdlib.h>

...

srand(time(NULL));

for(int i = 0;i<50;i++)
{
array = rand();
// or rand()% x to give value between 0 and x-1
}

matt

P.S. if you cant use rand, i know including string.h will have it.

 
To get the same random number sequence, re-use the same seed. You can supply it as the first parameter.

main (int argc, const char* argv[])
{
int seed = atoi (argv[1]);
srand (seed);

...
}

When you want a different seed, just run your programs with a different parameter.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top