mardi 14 mars 2017

Random numbers that do not match each other

I want to produce different numbers with c.

We can generate a random number using the stdlib library and the srand function.

For example; I want to produce a random number between 0 and 5.

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

int main(void)
{
int i;
int n = 4;
int array[3];

srand(time(NULL));

for(i = 0; i < n; i++)
{
   array[i] = rand() % 5; 
   printf("%d\n", array[i]);
}
return 0;


But the same numbers may coincide here.Like this;

Output:

   2

   4

   4

   1

How can I prevent this?




Aucun commentaire:

Enregistrer un commentaire