vendredi 2 décembre 2016

Random number generator produces the same random numbers [duplicate]

This question already has an answer here:

I'm trying to make a random number generator in C - surprisingly much harder compared to doing in Java with its standard library.

Here is my code. Now it does work however everytime I compile and run I get the same random numbers. So the numbers are random its just they dont change each method call. And this is something I need!

This method should return different numbers each time the method is called.

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

int rand(void);
void srand(unsigned int seed);
time_t time(time_t *t);
long random(void);

void generateRandomNumber()
{
    srand(time(NULL));
    //i tried rand() but thats worse all the numbers are the same 33,33,33 etc
    int randomNumber = random() % 100;
    printf("%d\n", randomNumber);
}

int main()
{
    for (int i = 0; i < 100; i++)
        generateRandomNumber();

}




Aucun commentaire:

Enregistrer un commentaire