On Mon, 30 Jul 2007 13:47:48 +0000, eliakim_online25 wrote:

> this is my first post here.....
> can someone teach me how to use rand and srand thanks in advance for
> your help.....
> V(^_^)V

srand() is used for seeding the random number, so it should have a seed
number, and rand() uses the seed number to create a random number.

One program could be this:

#include<stdio.h>
#include<string.h>
#include<time.h>

int main(void){
       int i=0;
       for(i=0;i<3;i++){
               srand(time(NULL));
               sleep(1);
               fprintf(stdout,"%d\n",rand());
       }
       return 0;
}

Leaving to this output
1736344033
1425524583
51979966

But look that if you put srand(time(NULL)); before the loop, you would
get this:
66552351
728401698
1801749096

This is the desired way of using srand() and rand().

Mateus

Reply via email to