I went to the beach at the weekend and played on one inpeticular game, the one were you pull the lever and XOO or w.e comes out, i was going to make one but thought i would ask if this was the best solution..

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    unsigned int firstColum, secondColum, thirdColum;
    
    /////////////////////////////////////////////////////////
    
    firstColum = rand() % 2 + 1;
    
    srand ( time(NULL) );
    
    /////////////////////////////////////////////////////////
    
    secondColum = rand() % 2 + 1;
    
    srand ( time(NULL) );
    
    /////////////////////////////////////////////////////////
    
    thirdColum = rand() % 2 + 1;
    
    srand ( time(NULL) );
    
    /////////////////////////////////////////////////////////
    
    if( firstColum == 1)
    cout << "X";
         else
    cout << "O";
    
    if( secondColum == 1)
    cout << "X";
         else
    cout << "O";
    
    if( thirdColum == 1)
    cout << "X";
         else
    cout << "O";
    
    getch();
}

EDIT: I've noticed i usually get same output

Dani AI

Generated

A few focused, practical notes to make the slot-style program reliable and simple to extend.

correctly pointed out why repeated seeding produced identical outputs: reseeding a generator repeatedly (especially with a time-based seed) often resets it to the same state. A more robust approach is to create a single random engine, seed it once, and draw from it for each reel. Prefer the facilities in <random>: they give better statistical properties and let you avoid common pitfalls like modulo bias.

Example pattern (pick an index per reel with one seeded engine):

#include <random>
#include <vector>
#include <string>
#include <iostream>

std::random_device rd;
std::mt19937 eng(rd());
std::vector<std::string> symbols = {"X", "O"};
std::uniform_int_distribution<int> dist(0, symbols.size() - 1);

for (int r = 0; r < 3; ++r)
    std::cout << symbols[dist(eng)];

Why this helps: uniform_int_distribution removes the bias that rand() % n can introduce; std::mt19937 is fast and reproducible if you use a fixed seed (handy for tests); std::random_device will often provide a nondeterministic seed, but its behavior is implementation-dependent — check the docs. See C++ random and uniform_int_distribution for details.

Practical extras: store symbols and payouts in arrays or a map so checking "XXX" or other patterns is just an index lookup; use a loop for any number of reels; use a fixed seed during unit tests to reproduce outcomes; and avoid platform-specific headers if portability matters. These steps make the game easier to maintain and extend for scoring, animation, or additional symbols.

Recommended Answers

All 5 Replies

>>Is this best solution?
No. srand() should only be called once during the lifetime of the program. Leave lines 12 and 14 but delete lines 18, 20, 24 and 26.

>>#include <conio.h>
That is non-standard function. If you want best solution than delete it.

line 45: delete it. use cin.get() instead.

lines 20 and 26 are using wrong variable names, so leave them but delete the preceeding calls to srand().

So are you saying i should only call srand once and use one variable for random number or..?

EDIT: I think i cracked that bit!

#include <iostream>

using namespace std;

int main()
{
    unsigned int firstColum, secondColum, thirdColum;
    
    /////////////////////////////////////////////////////////
    
    srand ( time(NULL) );
    
    firstColum = rand() % 2 + 1;
    
    /////////////////////////////////////////////////////////
    
    if( firstColum == 1)
    cout << "X";
         else
    cout << "O";
    
    firstColum = rand() % 2 + 1;
    
    if( firstColum == 1)
    cout << "X";
         else
    cout << "O";
    
    firstColum = rand() % 2 + 1;
    
    if( firstColum == 1)
    cout << "X";
         else
    cout << "O";
    
    cin.get();
}

Now, since the code in blocks 17-20 are repeated three times you can use a loop

for(int i = 0; i < 3; i++)
{

firstColum = rand() % 2 + 1;
    
    if( firstColum == 1)
    cout << "X";
         else
    cout << "O";

}

Sorry for this late reply but i've changed to firstColum, secondColum and thirdColum again, i'll post code when home and i've added options like if output = XXX you win $1, i'm planning to make it $0.10 a go and the total changes etc etc :)

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.