lundi 2 novembre 2020

How do I generate a unique set of random variables [duplicate]

My problem is really simple. In C, I am trying to create a set of random values to set for r, however whenever I run the code it generates the same numbers over and over again rather than a unique sequence of numbers on every iteration. How should I change the code to fix this?

My code:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main()
{
int r;
for (int i;i<5;i++)
{
    r=rand() % 10;
    printf("%d\n",r);
}
}

This code always returns the values 1,7,4,0,9. How can I make it so that it instead randomizes each on every successive use of the function?




Aucun commentaire:

Enregistrer un commentaire