C Reference function srand() initialize random number generator
This function of stdlib will initialize the random number generator that can be used with the rand() function.
Usage of srand():
void srand ( unsigned int seed );
The function srand() is used to initialize the pseudo-random number generator by passing the argument seed.
Often the function time is used as input for the seed.
If the seed is set to 1 then the generator is reinitialized to its initial value. Then it will produce the results as before any call to rand and srand.
Parameters:
An integer value to initialize the random number generator. Often the function time is used as input for the seed.
Return value:
There is nothing returned by srand.
Source code example of srand():
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main ()
{
printf ("Our first number: %d\n", rand() % 100);
srand ( time(NULL) );
printf ("Some random number: %d\n", rand() % 100);
srand ( 1 );
printf ("The first number again: %d\n", rand() %100);
return 0;
}
Output of the srand example program above:
Our first number: 41
Some random number: 4
The first number again: 41
#include
#include
#define ROW_SIZE 50
#define COL_SIZE 100
main()
{
int i,j;
int image[ROW_SIZE][COL_SIZE];
for (i=0;i<ROW_SIZE;i++)
{
for(j=0;j0)||(i47)||(i0)||(j97)||(j<99))
{
image[i][j]=0;
}//end if
} //end else
printf("%d ", image[i][j]);
printf("\n");
}// end for j
//printf("\n");
}// end for i
getchar();
}// end main
I don't know how to use the srand()& rand(), so when I run it, only "0"
I got the ans
Keep up the good work i will return often.
nice explanation …. keep the good works going this will help us a lot as a beginner programmer