Your loop is fine, your logic is off.
1st, please use code tags.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <iostream>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
using namespace std;
int main()
{
int min_ticket , max_ticket ;
int n1, n2, n3, n4, n5, n6;
cout << "Enter Minimum no. of ticket to be generated : ";
cin >> min_ticket;
cout << "Enter Maximum no. of ticket to be generated : ";
cin >> max_ticket;
int ticknum = min_ticket + rand() % (max_ticket - min_ticket);
cout << ticknum << endl;
//int n1, n2, n3, n4, n5, n6;
for (int c = 0; c < ticknum; c++)
{
n1 = 1 + (rand() % 47);
n2 = 1 + (rand() % 47);
n3 = 1 + (rand() % 47);
n4 = 1 + (rand() % 47);
n5 = 1 + (rand() % 47);
if (n1 != n2 && n1 != n3 && n1 != n4 && n1 != n5 &&
n2 != n3 && n2 != n4 && n2 != n5 &&
n3 != n4 && n3 != n5 && n4 != n5) {
break;
}
}
for (int i = 0; i < ticknum; i++)
{
n6 = 1 + (rand() % 27);
}
for (int i = 0; i < ticknum; i++)
{
cout << "Ticket " << n1 << " " << n2 << " " << n3 << " " << n4 << " " << n5 << " " << n6 << endl;
}
return 0;
}
|
Look at line 46. You're printing it ticknum times... but the value of n1,n2,n3 doesn't change
I think what you need here, is an array.
int n1, n2, n3, n4, n5, n6;
would become
int n[6];
Now you can do some work. Consider:
add #include <ctime>
after using namespace std;
Before main, a prototype.
int rollrnd();
as soon as you declare int main (){
Make your first command
srand(time(0));
This makes the random seed generator based on the current time.
in main you have;
1 2 3 4
|
for (int i=0; i<ticketsize; i++)
{
n[i] = rollrnd();
}
|
and your function after main ends with
would be something like:
1 2 3 4 5
|
int rollrnd()
{
n=rand()%47+1;
return n;
}
|
Now you have n[0], n[1],n[2],n[3] and so on randomized.
Instead of if (n1!=n2 && n1.....)
You can now
1 2 3 4
|
for (int i=0 to ticketnum-1)
if (n[i]==n[i+1]) {
n[i]=rollrnd(); // re-randomize it on the spot.
)
|
By the way "using namespace std;" is a bad practice too...
Now you have unlocked the power of arrays, and functions... Good luck!