dimanche 3 juillet 2016

Random number between x and y in a loop with no repeat [duplicate]

This question already has an answer here:

Oke I need to create random numbers between x and y. They are called min and max. I also put them in loop cause I want loop amount of numbers.The code works but sometimes it repeats numbers in results (same number twice) and I dont want it to happen.

I tried putting it into if or while but cannot manage to work it out.

I only wish to create random numbers between min and max in loop amount with no repeat.(loop also entered by me)

There are many random number code examples in this website but I couldnt find no repeat example.

This is what I wrote so far it works perfrectly but sometimes it repeats.

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

int main(int argc, char** argv)
{
    int r;
    int min;
    int max;
    int loop;
    srand(time(NULL));  
    scanf("%d",&min);
    scanf("%d",&max);
    scanf("%d",&loop);
    int i;
    for(i=0;i<loop;i++)
    {   
    r = (rand() % (max + 1 - min)) + min;
    printf("%d\n", r);
    }   
    return 0;
}

Any help to make it no repeat ?




Aucun commentaire:

Enregistrer un commentaire