jeudi 16 novembre 2017

using rand() in a function in c [duplicate]

This question already has an answer here:

I have made a function that creates a random number between 0 and 9 but when i call the function in main in a for loop with index i = 0 ; i < n ; i++ it prints 1 random number n types. but i want different values to be printed n times. here is the code which i made :-

    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    void random_variable() {
     int i;
     srand(time(NULL));
     i = rand()%10;
     printf("%d ",i);
    }
    main() {
     for(int j=0;j<10;j++) {
      random_variable();
     }
    }

output is this :-

please click on the link to see the output :- here instead of 8 ten times i needed 10 different values




Aucun commentaire:

Enregistrer un commentaire