Page 1 of 1

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

Posted: Tue Aug 08, 2006 9:09 pm
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

Posted: Tue Aug 08, 2006 9:21 pm
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.

Posted: Tue Aug 08, 2006 9:22 pm
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.

Posted: Tue Aug 08, 2006 9:49 pm
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

Posted: Tue Aug 08, 2006 9:51 pm
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

Posted: Tue Aug 08, 2006 11:09 pm
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.

Posted: Tue Aug 08, 2006 11:18 pm
by feyd

Code: Select all

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

//....

cout<< setw(10) << a /*...*/;

Posted: Tue Aug 08, 2006 11:34 pm
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

Posted: Tue Aug 08, 2006 11:41 pm
by feyd
it's avoidable. just need to add some more checking in the later do..whiles.

and it's

Code: Select all

..

Posted: Wed Aug 09, 2006 12:02 am
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