Hey guys, I'm writing a program that generates a random number. I have to find 6 random numbers (1-6) at least once and then terminate the loop when they have each been found. I tried using the "if..else if" loop but it still continues to run. Idk if I should be using a different loop or if I'm missing something in this one
printf("First 100 Random Numbers from URN13\n\n");
for (i = 1; i <= 100; i++)
{
srand(time(NULL));
n = rand() % 6 + 1;
printf("%d\n", n);
while (i <= 100)
{
if (n == 1)
{
OutputData(7);
Sleep(2000);
break;
}
else if (n == 2)
{
OutputData(2);
Sleep(2000);
break;
}
else if (n == 3)
{
OutputData(4);
Sleep(2000);
break;
}
else if (n == 4)
{
OutputData(6);
Sleep(2000);
break;
}
else if (n == 5)
{
OutputData(8);
Sleep(2000);
break;
}
else
{
OutputData(16);
Sleep(2000);
break;
}
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
int main()
{
srand( time(NULL) ) ;
// boolean flags to indicate if a number in [1,6] was generated.
// generated[0] is not used
bool generated[7] = { false, false, false, false, false, false, false };
int cnt_unique_generated = 0 ; // count of unique random numbers generated so far
while( cnt_unique_generated != 6 )
{
constint n = rand()%6 + 1 ;
if( generated[n] == false )
{
++cnt_unique_generated ;
generated[n] = true ;
}
}
puts( "each number in [1,6] was generated at least once" );
}