Page 1 of 1

Problem with rand()

Posted: Thu Apr 21, 2005 3:13 pm
by taj79
Hi,

Code: Select all

#include<stdio.h>
#include<stdlib.h>


main()
{
	int i,j;

	for(j=0;j<10;j++)
	{
		i = rand();
		printf("\n%d",i);
	}
}
I am running this in Microsoft Visual C++.
The problem is I am getting same set of values everytime I run ... what should I do to get different sets of value ?

Posted: Thu Apr 21, 2005 5:03 pm
by jakabjr
Hi. You must use the srand() function to set the random seed (read more about this in help pages or google). to get random numbers you should use srand(time(NULL)), with #include <time.h>, this will give different numbers for different moments of execution.
You should also read about using higher bits for better randomization.