This question already has an answer here:
- using rand to generate a random numbers 6 answers
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