I'm writing a simple number generator program and I'm trying to find an easier way to get a random number, store it then display it.
The catch is I don't want duplicated numbers (each need to be different).
here is what I have so far, it does work but I'll be working with more than just 3 numbers so this method would become very tedious to write up.
if(choice == 1)
{
system("cls");
num1 = rand() % 9 + 1;
num2 = rand() % 9 + 1;
num3 = rand() % 9 + 1;
if(num2 == num3 || num2 == num1)
{
num2 = rand() % 9 + 1;
}
if(num3 == num1 || num3 == num1)
{
num3 = rand() % 9 + 1;
}
if(num1 != num2 && num1 != num3)
{
numbers[0] = num1;
}
if(num2 != num3 && num2 != num1)
{
numbers[1] = num2;
}
if(num3 != num1 && num3 != num2)
{
numbers[2] = num3;
}
Also FYI this isn't a homework assignment, it's just something to do in my sparetime at work.
Thanks.