Thread: Generating Random Numbers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    40

    Generating Random Numbers

    I don't know alot about C++ but I know almost enough to make a slot machine game. The only thing I don't know is how to generate the random numbers. If you could give me some code or explain how that would be great.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    I have done a bit of work on it, it hasn't got the random conponent and isn't fully functional but why doesn't it compile? Here it is
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int return1=5;
        int return2=5;
        int return3=5;
        int coins=10;
        int input;
        
        cout << "Welcome to Slot Machine" << endl;
        cout << "You have 10 coins" << endl;
        cout << "Please press 1 to play, or 2 to exit and then hit enter";
        cin >> input;
        
        if(input==1)
        {
            cout << "You have chosen to play. Hit enter to input a coin and play";cin.get();
            cout << return1 << "   " << return2 << "   " << return3 << endl;
            if(return1==return2&&return1==return3)
            {
            coins=coins+10;
            cout << "You got the jackpot of 10 coins" << endl;
            cout << "You now have " << coins << " coins.";cin.get();
            cout << "Congratulations, you won.";
        }    
            else if(return1!=return2||return1!=return3||return2!=return3)
            {
            cout << "You lose, I am sillyI am sillyI am sillyI am sillyI am silly";
        }
        else
            {
            cout << "You have chosen to exit, please play again." << endl;   
        }    
            return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    That still means I need to learn how to make it random.

  4. #4
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    this is a example:

    Code:
    #include<cstdio>
    #include<ctime>
    
    main()
    {
    srand((unsigned)time(0));
    //use time() function as seed of rand() function
    int randomnum=rand()%1000;
    //define a int,and initialize it to a 'random number'
    // %1000 means this number wouldn't beyond 1000
    
    }

    blow me ... ...

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    The FAQ has a great tutorial on random numbers


    essentially, what you will want to do.. is to use the rand() function.. which will return an unsigned random number ranging from zero to RAND_MAX (which is a very large number). Anywhoo.. use the modulus operator as an upper bound on the range of numbers you wish to use...

    Code:
    slot_output1 = rand()%5    //will return a random integer between 0 and 4
    rand() may return a very large random number, but using the modulus operator will continously divide this larger number by 5 (in this example) until 5 is no longer able to fit into the randomly generated number.. and will return the remainder (if any)... and the remainder cannot be larger than 5

    also, don't forget to seed your random number generator.. as per the faq.


    edit: Hermitsky wins! (getting slow at me old age)
    Last edited by The Brain; 09-21-2004 at 07:03 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    Thankyou, but that still doesn't answer why it wont compile.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    Here's the edit with random, still bad but should work. Why won't compile?
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        srand((unsigned)time(0));
        int return1=rand()%10;
        int return2=rand()%10;
        int return3=rand()%10;
        int coins=10;
        int input;
        
        cout << "Welcome to Slot Machine" << endl;
        cout << "You have 10 coins" << endl;
        cout << "Please press 1 to play, or 2 to exit and then hit enter";
        cin >> input;
        
        if(input==1)
        {
            cout << "You have chosen to play. Hit enter to input a coin and play";cin.get();
            cout << return1 << "   " << return2 << "   " << return3 << endl;
            if(return1==return2&&return1==return3)
            {
            coins=coins+10;
            cout << "You got the jackpot of 10 coins" << endl;
            cout << "You now have " << coins << " coins.";cin.get();
            cout << "Congratulations, you won.";
        }    
            else if(return1!=return2||return1!=return3||return2!=return3)
            {
            cout << "You lose";cin.get();
        }
        else
            {
            cout << "You have chosen to exit, please play again." << endl;   
        }    
            return 0;
    }
    the debugger says that there is a syntax error at line 39, a line that doesn't exist in my compiler, devc++
    Last edited by pizzapie; 09-21-2004 at 05:56 AM.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    it works now! try it if you care, its still bad but if you like it that much im sure you can fix it up, ill post the perfected version when it is finished.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        srand((unsigned)time(0));
        int return1=rand()%10;
        int return2=rand()%10;
        int return3=rand()%10;
        int coins=10;
        int input;
        
        cout << "Welcome to Slot Machine" << endl;
        cout << "You have 10 coins" << endl;
        cout << "Please press 1 to play, or 2 to exit and then hit enter";
        cin >> input;
        
        if(input==1)
        {
            cout << "You have chosen to play. Hit enter to input a coin and play"<<endl;cin.get();
            cout << return1 << "   " << return2 << "   " << return3 << endl;cin.get();
        }    
            if(return1==return2&&return1==return3)
            {
            coins=coins+10;
            cout << "You got the jackpot of 10 coins" << endl;
            cout << "You now have " << coins << " coins.";cin.get();
            cout << "Congratulations, you won.";cin.get();
        }    
            else if(return1!=return2||return1!=return3||return2!=return3)
            {
            cout << "You lose, I am sillyI am sillyI am sillyI am sillyI am silly";cin.get();
        }
        else
            {
            cout << "You have chosen to exit, please play again." << endl;       
        }    
            return 0;
        }

  9. #9
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    absolutly, you missed something,
    this works fine:
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
        srand((unsigned)time(0));
        int return1=rand()%10;
        int return2=rand()%10;
        int return3=rand()%10;
        int coins=10;
        int input;
        
        cout<<"Welcome to Slot Machine"<<endl;
        cout<<"You have 10 coins"<<endl;
        cout<<"Please press 1 to play, or 2 to exit and then hit enter"<<endl;
        cin>>input;
        
        if(input==1)
        {
            cout <<"You have chosen to play. Hit enter to input a coin and play"<<endl;
    cin.get();
            cout <<return1<<"   "<<return2<<"   "<<return3<<endl;
            if(return1==return2&&return1==return3)
            {
            coins=coins+10;
            cout<<"You got the jackpot of 10 coins"<<endl;
            cout<<"You now have "<<coins<<" coins."<<endl;
    cin.get();
            cout<<"Congratulations, you won."<<endl;;
        }    
            else if(return1!=return2||return1!=return3||return2!=return3)
            {
            cout <<"You lose, I am sillyI am sillyI am sillyI am sillyI am silly"<<endl;
        }
        else
            {
            cout<<"You have chosen to exit, please play again."<<endl;   
        }    
            return 0;
    }
    }

    blow me ... ...

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    why include those two libraries? it works without them

  11. #11
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    time() is defined in ctime,rand() is defined in cstdlib

    blow me ... ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Criteria based random numbers
    By alkisclio in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2006, 12:29 PM
  4. Random Number Generating
    By K.n.i.g.h.t in forum C Programming
    Replies: 9
    Last Post: 01-30-2005, 02:16 PM
  5. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM