jeudi 22 décembre 2016

Suggestion about unique values generation

I want to generate a series of "random" unique numbers, for use at a card game! These numbers should be between 0 and 81. I don't care about security or speed at this stage, i just want something simple to have the work done.

In my code below, i have managed to create 2 unique random numbers in the array that holds them, but the rest 10 numbers don't change but stay -1 that was the initial value.. I have found more accurate ways for random number generation, but i will check them out at a later stage!

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




int getRandomNumber(int Min, int Max)
{
    double rnd= (double)rand()/((double)RAND_MAX+1);
    return   (int)(rnd*(Max-Min+1))+Min;
}
int main()
{
    int j,counter,temp,deck[13];
    srand(time(NULL));
    int i;
    counter=1;
    for (i=0;i<12;i++)
        {deck[i]=-1;
        temp = getRandomNumber(0,81);

        for (j=0;j<=i;j++)
            {if (temp==deck[j])
                {counter=0;}
            if (counter!=0)
                deck[i]=temp;
            }
        }
    for(i=0;i<12;i++)
        printf("%d ",deck[i]);

}




Aucun commentaire:

Enregistrer un commentaire