What a painfull problem this is!!!! any advice?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

What a painfull problem this is!!!! any advice?

Post by matt1019 »

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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

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.
(#10850)
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post by matt1019 »

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.
ok.... I am getting SOMEWHERE with this.

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
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post by matt1019 »

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.
Hi feyd,

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
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post by matt1019 »

Ok guys,

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);
}
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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

do
{
  x = rand_int(1,10);
}
while(a == x);
a = x;

//....

cout<< setw(10) << a /*...*/;
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post by matt1019 »

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:

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;
hmm, and the output still has first #'s same.... here's what I mean:
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
look at 1st row, 1st col --- and 3rd row 1st col.

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 :P

please change it as you see fit :)

-Matt
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's avoidable. just need to add some more checking in the later do..whiles.

and it's

Code: Select all

..
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post by matt1019 »

ok, thanks feyd :)

and thank you all for your help!!!

Without your pointers, I would have NEVER grasped the "Logic"/"Concept" behind this (how to avoid same # when generating random #s)


I can finally go back to my php project!! :o :o

-Matt
Post Reply