Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] Logic Fail?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Wed Oct 20, 2010 9:50 am    Post subject: [C++] Logic Fail? Reply with quote

This is what I'm trying to do...

Part a is irrelevant, I'm just putting it there for contextual reasons.
I'm doing part b right now. So I have it generating random values for the userTicket and the winningTicket, and the game plays itself 50,000 times.
The problem is...I think my logic is flawed somewhere, because no matter what, I can NEVER get 5 matches. Even with a million run-throughs.

Also, the Law of Large numbers basically just says that if you have a fuck ton of trials of any probability type problem, their probabilities will head towards their true statistical probability.

Quote:

a. Design a simple lottery game. (Use vectors and a modular design.)

The winning ticket will consist of 5 winning digits from 0 to 9 with no repeats. For example,

winningTicket = 4, 0, 7, 9, 2

Allow the user to choose 5 numbers, with no repeats. For example,

userTicket = 5, 4, 9, 1, 0

Here the user has 3 matches. A user wins if he has 5 matches. Display to the user the number of matches and ask if he wishes to play again.

b. Automate the game and use the law of large numbers to calculate the probability of 0, 1, 2, 3, 4, and 5 matches.



So anyways, here is my code. Feel free to point out how shitty my code is, I don't mind I guess. But I'm more curious as to why I can't get 5 matches ever. Thanks for any help.

Code:
#include <string>
#include <vector>
#include <ctime>
#include <iostream>
using namespace std;




//Function Prototypes
bool runAgain(void);
bool isClear(int x, vector<int> y, int elment);
vector<int> createWinningTicket(void);
void printVector (vector<int> myVect);
vector<int> createUserTicket(void);


int main (){

   cout << "User input will be instead be automaticly replaced with random numbers" << endl;
   cout << "Please wait while the results of 50,000 calculations are computed.." << endl;
   srand( unsigned (time(0)) );
   
   vector<int> match(6);

   for(int count = 0; count  < 50000; count++){


      vector<int> winningTicket = createWinningTicket();
      vector<int> userTicket = createUserTicket();
      

      

   
      int matches = 0;

      for(int g=0;unsigned(g) < userTicket.size(); g++){
         for(int k=0;unsigned(k) < winningTicket.size(); k++){
            if(userTicket[g] == winningTicket[k])
               matches++;
         }
      }

   
      for( int r=0;r<5;r++){
         if(matches == r)
         match[r]++;
      }


   }

   int maxtotal = match[0] + match[1] + match[2] + match[3] + match[4] + match[5];

   cout << "A total of 0 matches occurred " << double(match[0])/maxtotal << ", or " << 100.0*( double(match[0])/maxtotal) << "% of the time.\n";
   cout << "A total of 1 matches occurred " << double(match[1])/maxtotal << ", or " << 100.0*( double(match[1])/maxtotal) << "% of the time.\n";
   cout << "A total of 2 matches occurred " << double(match[2])/maxtotal << ", or " << 100.0*( double(match[2])/maxtotal) << "% of the time.\n";
   cout << "A total of 3 matches occurred " << double(match[3])/maxtotal << ", or " << 100.0*( double(match[3])/maxtotal) << "% of the time.\n";
   cout << "A total of 4 matches occurred " << double(match[4])/maxtotal << ", or " << 100.0*( double(match[4])/maxtotal) << "% of the time.\n";
   cout << "A total of 5 matches occurred " << double(match[5])/maxtotal << ", or " << 100.0*( double(match[5])/maxtotal) << "% of the time.\n";

        //I just had this for testing purposes, irrelevant.
   cout << match[0] << endl;
   cout << match[1] << endl;
   cout << match[2] << endl;
   cout << match[3] << endl;
   cout << match[4] << endl;
   cout << match[5] << endl;
   //---------------------------------------------------



   return(0);
}







// Function Implementations


bool isClear(int x, vector<int> y, int elment){

   for(int j = 0; unsigned(j) < y.size(); j++){
      if(j==elment) continue;
      if( y[j] == x ) return false;
   }
   
   return true;
}


vector<int> createWinningTicket(void){

   vector<int> winTick(5);

      for(int i = 0; i < 5; i++){
         
         int tempNum;
         
         do{
            tempNum = rand() % 10;
            winTick[i] = tempNum;
         }
         while(!isClear(tempNum, winTick, i));
         
      }

   return winTick;
}

void printVector (vector<int> myVect){
   
   for(int f = 0; unsigned(f) < myVect.size(); f++)
      cout << myVect[f] << " ";
}


vector<int> createUserTicket(void){

   vector<int> winTix(5);

      for(int j = 0; j < 5; j++){
         
         int tempStore;
         
         do{
            tempStore = rand() % 10;
            winTix[j] = tempStore;
         }
         while(!isClear(tempStore, winTix, j));
         
      }

   return winTix;
}


_________________
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Wed Oct 20, 2010 10:54 am    Post subject: Reply with quote

Something like this using more stl and a lambda expression:

Code:

#include <Windows.h>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

std::vector< unsigned char > vWinningTicket; // Overall winning ticket.
std::vector< unsigned char > vCurrentTicket; // Users current ticket.

bool isClear( const std::vector< unsigned char >& vec, unsigned char value )
{
    std::vector< unsigned char >::const_iterator iter = std::find( vec.begin(), vec.end(), value );
    if( iter == vec.end() )
        return true;
    return false;
}

bool createTicket( std::vector< unsigned char >& vec )
{
    vec.clear();

    for( int x = 0; x < 5; x++ )
    {
        unsigned char tempVal = rand() % 10;
        while( !isClear( vec, tempVal ) )
        {
            tempVal = rand() % 10;
        }
        vec.push_back( tempVal );
    }

    return true;
}

void printVector( std::vector< unsigned char > vec )
{
    std::ostream_iterator< int > output( std::cout, " " );
    std::copy( vec.begin(), vec.end(), output );
}

int __cdecl main( int argc, TCHAR* argv[] )
{
    srand( GetTickCount() );

    std::cout << "50,000 calculations will now be processed, please wait..." << std::endl;
    std::cout << "Creating winning ticket.." << std::endl;

    // Create winning ticket.
    createTicket( vWinningTicket );
    std::cout << "The winning ticket is: ";
    printVector( vWinningTicket );
    std::cout << std::endl;

    for( int x = 0; x < 50000; x++ )
    {
        // Create users ticket.
        createTicket( vCurrentTicket );

        int nMatches = 0;
        std::for_each( vCurrentTicket.begin(), vCurrentTicket.end(),
            [ &nMatches ]( unsigned char value )
            {
                if( !isClear( vWinningTicket, value ) )
                    nMatches++ ;
            }
        );

        std::cout << "Current ticket: ";
        printVector( vCurrentTicket );
        std::cout << std::endl;

        std::cout << "Total matches: " << nMatches << std::endl;
        std::cout << std::endl;

        if( nMatches == 5 )
        {
            std::cout << "Winning ticket! Congradulations, you won!" << std::endl;
            std::cin.sync();
            std::cin.ignore();

            break;
        }
    }

    return 0;
}


I only did the first part since user input isn't much a change from this except checking incoming input against isClear and so on. Some parts could be done better / different but I did this in about 5 minutes so didn't really put effort into it.

If this is for school, you probably aren't allowed to use some things I did, as well as the lambda since its part of the new 0x standard and isn't fully released. (But if you have VS2010 you can use them already.)

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
justa_dude
Grandmaster Cheater
Reputation: 23

Joined: 29 Jun 2010
Posts: 891

PostPosted: Wed Oct 20, 2010 3:11 pm    Post subject: Reply with quote

You don't state whether or not the numbers must appear in order to be a valid solution. This is important information, and Wiccan's answer depends on it. Your check for vector equality is rather contrived, which I'm guessing is supposed to catch the case where the numbers are not in order. If the order is not important, I would use the sort function from <algorithm> before comparing using the equal function from the same header (if I were required to use vectors instead of sorted structures, that is). Even if you still iterate the vectors manually, it's a lot cleaner and checking for duplicate elements is a lot faster. Regardless of which you choose, you should definitely add some comments around that region.

As an aside, casing the for-loop guards to unsigned is probably bad practice as well, since int is only 2-bytes on some systems in use and I believe you could, in practice, overflow.

Cheers,
add
Back to top
View user's profile Send private message
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Wed Oct 20, 2010 9:31 pm    Post subject: Reply with quote

Wow..I just needed to change

Code:
for( int r=0;r < 5 ;r++){
         if(matches == r)
         match[r]++;
      }


to

Code:
for( int r=0;r < match.size() ;r++){
         if(matches == r)
         match[r]++;
      }



And it works fine now.

And yes, its for school, so I can't really use things "I don't know" of yet.
Thanks guys, both of you, for the help <3

_________________
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites