Hey guys,

I'm struggling with yet another problem. The goal is to run 1000 "draws" of three numbers but the catch is, the three numbers can't be repeating. So, no 4 1 4 or 7 7 7 or 8 1 8 and so on and so forth. Every line of three numbers must have the three numbers be from 1-8.

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
    int Yellow = 1; 
    int Blue = 2;
    int Red = 3; 
    int Purple = 4; 
    int Orange = 5; 
    int Green = 6; 
    int Maroon = 7;  
    int Black = 8;
    int tally_total = 1000;
    int tally[tally_total];

    for(int i = 0; i < tally_total; i++)
    {
    tally[i] = (rand()%8)+1;   
    tally[i+1] = (rand()%8)+1;
    tally[i+1] != tally[i];
    
    while(tally[i+1] == tally[i])
    {
    if(tally[i+1] == tally[i])
    {
                  tally[i+1] = (rand()%8)+1;
                  continue;}
    }
    tally[i+2] = (rand()%8)+1;
    tally[i+2] != tally[i] || tally[i+1];
    
    while(tally[i+2] == tally[i])
    {
         if(tally[i+2] == tally[i])
         {
                  tally[i+2] = (rand()%8)+1;
                  continue;
         }
    }
    
    while(tally[i+2] == tally[i+1])
    {
	     if(tally[i+2] == tally[i+1])
	     {
	              tally[i+2] = (rand()%8)+1;
	              continue;
		 }
	}
    
    
    if(tally[i+1] == tally[i])
    cout << "NUMBERS REPEATED";
    if(tally[i+1] == tally[i+2])
    cout << "NUMBERS REPEATED";
    if(tally[i] == tally[i+2])
    cout << "NUMBERS REPEATED";
    
    cout << tally[i] 
         << " " 
         << tally[i+1] 
         << " " 
         << tally[i+2]
         << endl;
    }
     
    getchar();
    getchar();
    return 0;    
}

Recommended Answers

All 3 Replies

tally[i+2] != tally[i] || tally[i+1] (line 34) is not doing what you want it to do. You need (tally[i+2] !=tally[i]) || (tally[i+2] !=tally[i+1]) .

Have a test condition in your for loop.

In psuedocode :

for i = 0 untill MAX{
pick1 = random number
pick2 = random number
while(pick2 == pick1 ) pick2 = another random number
pick3 = random number
while(pick3 == pick2 or pick3 == pick1) pick3 = another random number
}

First, thanks to both jonsca and firstperson!

I rewrote the code AGAIN and got it to work! Firstperson, your psuedocode was exactly what I needed. Sorry for the long time offline, but my family thinks I'm becoming addicted from CSC haha. I spent about three hours of work to JUST GET the duplicates to go away, and couldn't manage to do it myself. Pathetic, I know. Again, thanks a bunch to both of you! Topic solved!

EDIT: If someone has some time and could PM me, I'd love to know why my original code didn't work fully. Out of 1000 runs, it had 7 repeating numbers per roll. Why was that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.