What a painfull problem this is!!!! any advice?
Moderator: General Moderators
What a painfull problem this is!!!! any advice?
Hi guys,
I am working on a c++ program.... generating 3 random #s (FROM 1-10) for however many times user asks for it.
so for example, i say i want to buy 3 tickets,
the program would generate:
3 8 6
4 7 8
1 9 2
(of course, I made up the #'s.... in the program, they would be generated randomly).
Generating random numbers is not a problem where I am stuck.
The problem lies in this criteria:
the first # cannot be the same as first # from previous row.
the second # cannot be the same as the second # from the previous row.
the third # cannot be the same as the third # from the previous row.
Here's what I mean:
3 8 6
3 5 4
cannot be passed as valid since 3's are repeating.
3 8 6
4 8 4
cannot be passed as valid since 8's are repeating.
and same goes for the third #.
I do not want to ask you for "code" but rather, a pointer... or concept explanation as to how do you suggest I go about solving this.
Any Help appreciated, guys! Even if the help is in terms of php.
-Matt
I am working on a c++ program.... generating 3 random #s (FROM 1-10) for however many times user asks for it.
so for example, i say i want to buy 3 tickets,
the program would generate:
3 8 6
4 7 8
1 9 2
(of course, I made up the #'s.... in the program, they would be generated randomly).
Generating random numbers is not a problem where I am stuck.
The problem lies in this criteria:
the first # cannot be the same as first # from previous row.
the second # cannot be the same as the second # from the previous row.
the third # cannot be the same as the third # from the previous row.
Here's what I mean:
3 8 6
3 5 4
cannot be passed as valid since 3's are repeating.
3 8 6
4 8 4
cannot be passed as valid since 8's are repeating.
and same goes for the third #.
I do not want to ask you for "code" but rather, a pointer... or concept explanation as to how do you suggest I go about solving this.
Any Help appreciated, guys! Even if the help is in terms of php.
-Matt
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
ok.... I am getting SOMEWHERE with this.arborint wrote:Save the values of the previous row and test the values against them each time -- regenerate if it matches. Initialize the previous values to some invalid value.
I will post my final solution here in about 20 or so mins.... after I do additional things to it.
of course, the source will be in c++
-Matt
Hi feyd,feyd wrote:An array of ints (or chars) who's length is the same as a single row's. This is your previous random bucket. Then a for.. surrounding a do..while that iterates over each bucket randomly choosing until all buckets have been replaced.
Your suggestion was the first thing I tried.... (as I was going reply by reply)
I couldn't get it to work though.
Thank you for your help, by the way.
And thanks to you too, arborint
-Matt
Ok guys,
here's my version of what I interpreted from arborints suggestions:
The painfull thing is, there still exists the repition of same #'s ;(
BUT, logic wise, is this correct?
Thank you for your help,
-Matt
feyd | switched to C so I could read it.
here's my version of what I interpreted from arborints suggestions:
Code: Select all
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cmath>
#include <string>
using namespace std;
int rand_int(int a, int b)
{
return rand()%(b-a+1) + a;
}
int main()
{
int ans, tossone, tosstwo, tossthree, a, b, c, x, y, z;
cout<<"Please Enter How many Lottery tickets you want to buy: ";
cin>>ans;
srand((unsigned)time(0));
tossone = rand_int(1,10);
tosstwo = rand_int(1,10);
tossthree = rand_int(1,10);
a = tossone;
b = tosstwo;
c = tossthree;
cout<<"three random numbers are: "<<endl;
cout<<endl;
cout<<endl;
cout<<setw(10)<<tossone<<setw(10)<<tosstwo<<setw(10)<<tossthree<<endl;
for (int i=1; i<ans; i++){
if (a == tossone){
x = rand_int(1,10);
}
if (b == tosstwo){
y = rand_int(1,10);
}
if (c == tossthree){
z = rand_int(1,10);
}
cout<<setw(10)<<x<<setw(10)<<y<<setw(10)<<z<<endl;
}
return (0);
}
BUT, logic wise, is this correct?
Thank you for your help,
-Matt
feyd | switched to C so I could read it.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
do
{
x = rand_int(1,10);
}
while(a == x);
a = x;
//....
cout<< setw(10) << a /*...*/;Hi Feyd,
Thank you very much for your help
I have updated my for loop as per your suggestion and the rest of the code/sourcecode rather, remains the same as I posted above:
hmm, and the output still has first #'s same.... here's what I mean:
do you think this is unavoidable? or is there a way around this?
Thanks once again,
-Matt
EDIT:
Sorry, I tried [c] [/c] tags, but I guess it's for Mod's only.... therefore, I went back to tags 
please change it as you see fit
-Matt
Thank you very much for your help
I have updated my for loop as per your suggestion and the rest of the code/sourcecode rather, remains the same as I posted above:
Code: Select all
for (int i=1; i<ans; i++){
do{
x = rand_int(1,10);
}while(a == x);
a = x;
do{
y = rand_int(1,10);
}while(b == y);
b = y;
do{
z = rand_int(1,10);
}while(c == z);
c = z;
cout<<setw(10)<<a<<setw(10)<<b<<setw(10)<<c<<endl;look at 1st row, 1st col --- and 3rd row 1st col.Please Enter How many Lottery tickets you want to buy: 5
three random numbers are:
4 10 4
6 9 7
4 8 4
9 1 3
1 8 7
do you think this is unavoidable? or is there a way around this?
Thanks once again,
-Matt
EDIT:
Sorry, I tried [c] [/c] tags, but I guess it's for Mod's only.... therefore, I went back to
Code: Select all
please change it as you see fit
-Matt
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
it's avoidable. just need to add some more checking in the later do..whiles.
and it's
and it's
Code: Select all
..