samedi 26 mars 2016

genrate different random numbers of limited range each time

#include<iostream.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>

int isUnique(int arr[], int n, int index)
{
    for(int i=0;i<index;i++)
    if(arr[i] == n)
    return 0;
    return 1;
}

void main()
{
clrscr();
int n,arr[5];
srand(time(0));//reset the random values generated //
for(int i=0;i<5;i++){
n=rand()%9+49;
while(!isUnique(arr,n,i))
n=rand()%9+49;
cout<<n<<" ";
getch();
}

/*its my try for generating a set of numbers between 49-58 such that new number gnerated should be different from previous but sometimes it still generates 1 duplicate number output like :49 50 57 49 51 please help me out or share me a new code instead that can do that(using turbo c++) */




Aucun commentaire:

Enregistrer un commentaire