samedi 4 mars 2023

Why is srand used only in main function? [duplicate]

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

int intRandom(int min, int max){
    int t;

    t=min+rand()%(max-min+1);
            
    return t;
}

int main(){
    srand(time(NULL));
    int total;

    do {
        printf("Enter the total: ");
        scanf("%d",&total); 
    }while (total<2 || total>12);

    int count=1;
    int x,y;

    do{
        x=intRandom(1,6);
        y=intRandom(1,6);

        printf("Result of throw %d : %d+%d\n ",count,x,y);
        count++;
    }while (x+y!=total);

    return 0;
}

I need a reasonable explanation as to why we don't use srand in a function other than main.

And Could you explain to me why we have to add 1 to this part of the formula generating the random number (max-min+1) instead of just (max-min) in intRandom function?




Aucun commentaire:

Enregistrer un commentaire