Click to See Complete Forum and Search --> : why srand() is usued while calling a rand() function? [C/C++]


blue_gene
04-17-2004, 03:04 AM
why srand() is usued while calling a rand() function in C/C++.

the syntx is srand(time(NULL))...what it does ?

instead of above syntax if i use srand ( big_int_number)...will it be valid ?

i dont understand why srand() is called ? and why time is also inside ?

any explanation ?

phlipant
04-17-2004, 03:17 AM
the srand function sets the seed of the random number generator. they recommend you use a different seed value on different passes.

The following:

int x, y;

srand(10);
x = rand();
y = rand();

would produce the same results as:

int x, y, s = 10;

rand_r(&s, &x);
rand_r(&s, &y);

phlipant
04-17-2004, 03:21 AM
these have been depreciated, by the way. you should use srand48 and drand48.

phlipant
04-17-2004, 03:28 AM
Originally posted by blue_gene

i dont understand why srand() is called ? and why time is also inside ?

any explanation ?

time is inside so that each time you run srand you get a different seed to start your random number generator, since the time has changed.

blue_gene
04-17-2004, 04:42 AM
hi, can you answer these 3 questions....i need to understand these three questions.

question 1 :

do i need to intialize srand() every time inside a loop before calling rand() ? or do i need to intialize only once ?


i.e



while(some_condition)
{
srand(); // every time initialized.
double x = rand(); // x is the random number.
}




OR


srand(); // initialized only once

while(some_condition)

double x = rand(); // x is the random number



which one is correct ?



question 2: its really a peculiar syntx to write time( NULL)....why NULL ?




question 3:


my code


#include <stdio.h>
#include <string.h>
#include<time.h>
#include<stdlib.h>
int main()
{

int i =0;

while(i<10)
{

srand(time(NULL));

double x = rand();

printf("%f\n\n",x);
i++;


}




my output >
859513064.000000

859513064.000000

859513064.000000

859513064.000000

859513064.000000

859513064.000000

859513064.000000

859513064.000000

859513064.000000

859513064.000000

can you call these are random numbers ? ...i suppose not. using time why it is giving wrong result ?

thanks

bwkaz
04-17-2004, 10:00 AM
man 3 rand (note also the comments applying to srand)
man 2 time

Your questions will be answered. Or at least, they should be.

Specifically, from rand(3):

The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value. If you use the same seed value, you wil get exactly the same sequence of random numbers.

And, from time(2):

SYNOPSIS

#include <time.h>

time_t time(time_t *t); NULL is passed to the time() function because the time() function's prototype requres a pointer to a time_t structure. Also, in the DESCRIPTION section of the time(2) manpage:

If t is non-NULL, the return value is also stored in the memory pointed to by t. You don't need the return value stored anywhere, you only need the return value. So you pass NULL.

For your third question, refer to the section above from the rand(3) manpage in bold. Also note that time() returns the time in seconds, which almost assuredly won't change while your piddly little loop is running. So you're reinitializing the seed to the exact same value every time through the loop, which means you're going to get the exact same sequence of random numbers (actually only one) every time through the loop.

rand() is not a random number generator, it's a pseudo-random number generator. If you know the seed, you know the sequence of numbers, because the algorithm is deterministic.

phlipant
04-17-2004, 10:53 AM
you need to place the srand call, before while, not inside the loop. you also need scale the result to reflect what kind of random system you need.

here is a very old program that generates unique random numbers, ranging between 1 and 2000.

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

#define NUM 100
int j[NUM];

void main()
{
int i,k,n,im,flag=0;

n=2000;

srand((unsigned) time(NULL));
for(i=0;i<NUM;i++)
{
restart: j[i]=rand();
im=j[i]/n;
j[i]=j[i]-n*im;
flag=0;
for(k=0;k<i;k++)
{
if(j[k]==j[i]) flag=1;
}
if(flag) goto restart;
printf("%d\n",j[i]);
}
}


note how the result of rand is processed to produce the result required.

phlipant
04-17-2004, 12:55 PM
i said that wrong, it goes from 0 to 1999.

bwkaz
04-17-2004, 01:05 PM
Ah, indentation. Much easier on my eyes... :p

As for the goto, get rid of it. Use continue; instead.

The continue statement causes the loop to skip to the next iteration. Since you need to restart the current one, though, you can decrement i just before doing a continue.

For that matter, you don't need to finish the inner loop when setting the separate flag; you can just do:

if(j[k] == j[i]) {
flag=1;
break;
} inside the inner for loop. This will make it so that no more loop iterations run (until the outer loop runs around again).

You can also use the modulo operator rather than calculating it yourself. In other words:

j[i]=rand();
im=j[i]/n;
j[i]=j[i]-n*im; becomes just:

j[i] = rand() % 2000;

phlipant
04-17-2004, 02:59 PM
as i said, it is very old code.