Hi all!
Newbie question,
Im currently working on an assignment for college, part of my prog needs to generate a random quantity <4 , of random numbers <7 if that makes sense.
but if the random generator outputs the same number twice it should restart until it gives me a series of different numbers.
Quite tricky to explain but the part im stuck on is when it generates 2 numbers the same how do i tell it to restart?
I.E if(count>0)
restart loop
Is there a redo command in c?
theres hopefully a simple solution but i cant find anything in my textbooks.
Heres my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int i,x,y,count=0,p,array[3],array2[5];
srand(time(NULL));
x = rand()%4+1; /* generate number of choices*/
printf("%d",x);
printf("\n\n");
for(i=0;i<x;i++)
array[i]=rand()%7; /*create array of number of choices & fill with menu selections*/
for(i=0;i<x;i++)
{
for(p=i+1;p<x;p++)
{
if(array[p]==array[i])
count++;
}
if(count>0) /* if count >0 redo*/
for(i=0;i<x;i++)
printf("%d\n",array[i]);
printf("\n");
printf("\n count=%d \n \n",count);
system("pause");
}
}