vendredi 26 juin 2020

how to make random values in one function be same for each run while random values in another function be different for each run

I searched the net and it showed the srand() method for generating new random values for each run , while i was using the simple rand() method for generating same random numbers in each run , but i want both these functionalities at different places in the program.
This is what i have tried:

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

void first()
{ 
    printf("these random values should be same for every run\n");
    for(int i = 0; i<4; i++)
        printf(" %d ", rand()%4);
    printf("\n");

} 
void second()
{ 
    srand(time(0));

    printf("these random values should be different for every run\n");
    for(int i = 0; i<4; i++)
        printf(" %d ", rand()%4);
    printf("\n");
} 

int main(void)
{


    first();
    second();
    return 0;
}

please help.




Aucun commentaire:

Enregistrer un commentaire