dimanche 14 octobre 2018

srand(time(NULL)) function

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

int main( void){

    int x = rand()%100;
    printf("%d\n", x);
    return 0;
}

The code above generates a random number correctly. Is this correct? But, other sources always include library and srand(time(NULL)). Why do we have to include include library and srand(time(NULL))? Are there any reasons to include?

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

int main( void){
    srand(time(NULL));
    int x = rand()%100;
    printf("%d\n", x);
    return 0;
}




Aucun commentaire:

Enregistrer un commentaire