Thread: Randomization problem

  1. #1
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20

    Question Randomization problem

    Hi again, sorry to keep posting questions in this forum but i have nowhere else to find answers . I'm now trying to make a simulation of a slot machine but for some reason which i should probably already know the slot machine will always have the same three results.
    I am using the time function for randomness which i think is the problem as the three results are calculated at pretty much the same time although i do not know how to fix this.

    Here is the code the my unfinished program:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    
    
    using namespace std;
    
    
    int money = 1000;
    int bet;
    
    
    void slotResult()
    {
        int counter = 0;
        while (counter < 3)
        {
            srand (time(NULL));
            int i = rand() % (3 - 1 + 1) + 1;
    
    
            switch (i)
            {
            case 1:
                {
                    cout <<"BALL\t";
                    break;
                }
            case 2:
                {
                    cout <<"COIN\t";
                    break;
                }
            case 3:
                {
                    cout <<"STICK\t";
                    break;
                }
            }
            counter++;
        }
    }
    
    
    int main ()
    {
        cout <<"YOU HAVE $1000 TO SPEND ON THE SLOT MACHINE";
        cout <<"\n\n";
        cout <<"INSTRUCTIONS: PLACE A BET, IF YOU GET 3 MATCHING ITEMS YOU RECIEVE QUADRUPLE THE \nAMOUNT WHICH YOU BET, IF YOU DO NOT YOU LOSE YOUR BET";
        cout <<"\n\n\n\n";
        cout <<"ENTER BET: ";
        cin >>bet;
        slotResult();
    
    
    }
    Can someone please tell me what i am doing wrong here?

    thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should call srand once, near the start of the main function. Otherwise, you would end up re-seeding the PRNG before each call to rand, which doesn't work especially when your seed is based on the current time.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20
    Ok thanks a lot

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unless you are stuck with an older compiler, consider using the new random library in C++11: <random> - C++ Reference
    std::rand is very poor at creating a uniform distribution and a little tricky into manipulating the values into a certain range.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Randomization
    By Burns111 in forum C Programming
    Replies: 4
    Last Post: 11-27-2012, 01:05 PM
  2. randomization in c++?
    By zanderela in forum C++ Programming
    Replies: 2
    Last Post: 03-21-2008, 02:18 AM
  3. Randomization
    By ldb88 in forum C++ Programming
    Replies: 12
    Last Post: 06-21-2006, 12:36 AM
  4. Arrrays, Loops, and Randomization
    By myrddin in forum C Programming
    Replies: 4
    Last Post: 05-21-2003, 04:01 AM
  5. Randomization
    By blackmagic in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2002, 11:15 AM

Tags for this Thread