Generating Random Numbers

This is a discussion on Generating Random Numbers within the C++ Programming forums, part of the General Programming Boards category; I don't know alot about C++ but I know almost enough to make a slot machine game. The only thing ...

  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 08: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 06: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 ... ...

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Just because it works without them on one compiler (which only means that <iostream> eventually includes them), doesn't mean it will still work on another.

    The third check here is redundant:
    Code:
    if(return1!=return2||return1!=return3||return2!=re  turn3)
    If return1 == return2 and return1 == return3 (which is the precondition for the third part to be even evaluated), then return2 == return3 and the third inequality is always false.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    i see your point, but it doesn't matter really. Also, once the random numbers values are declared, they dont change. I put in a loop so the prog is half good but the same three numbers pop up everytime. I know why, but i dont know how to make it change everytime. heres the code and tell me where to fix it.
    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 "<<endl;
        cin >> input;
        
        do{
        if(input==1)
        {
            cout << "You have chosen to play. Hit enter to input a coin and play"<<endl;cin.ignore();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)
            {
            coins=coins-1;
            cout << "You lose. You now have " << coins << " coins.";cin.get();
        }
        else if(return1==return2||return1==return3||return2==return3)
        {
            coins=coins+3;
            cout << "You win 3 coins. You know have " << coins << " coins.";cin.get();
              srand((unsigned)time(0));
        }    
        else
            {
            cout << "You have chosen to exit, please play again." << endl;cin.get();       
        }
        }while(coins!=100||coins>=1);
        cout << "You have either won the game by reaching 100 coins or lost all your money.";cin.get();
            return 0;
        }

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    i fix it up even more, but still many problems. The obvious one that the three numbers never change exists, but i can fix it. Its just that i cant get the loop to work correctly eg if you hit more than 100 coins or get less than 1 coin it says the same message cause i dont know how to get the loop to functions so you get a different response if you lose and if you win. here it is
    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 "<<endl;
        cin >> input;
        
       while(coins>0&&coins<100)
       {
        if(input==1)
        {
            cout << "You have chosen to play. Hit enter to input a coin and play"<<endl;cin.ignore();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)
            {
            coins=coins-1;
            cout << "You lose. You now have " << coins << " coins.";cin.get();
        }
        else if(return1==return2||return1==return3||return2==return3)
        {
            coins=coins+3;
            cout << "You win 3 coins. You know have " << coins << " coins.";cin.get();
        }    
        else if(coins==100)
        {
            cout << "Congratulations. You won.";cin.get();
        }    
        else if(coins<1)
        {
            cout << "You are broke.";cin.get();
        }    
        else
            {
            cout << "You have chosen to exit, please play again." << endl;cin.get();       
        }
    }    
           cout << "You have either won the game by reaching 100 coins or lost all your money.";cin.get(); 
            cin.get();return 0;
        }

  15. #15
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    Hi pizzapie,

    Code:
    else if(coins==100)
        {
            cout << "Congratulations. You won.";cin.get();
        }    
        else if(coins<1)
        {
            cout << "You are broke.";cin.get();
        }
    Looks like this part of your if/elseif/else ladder would only ever execute if the previous three conditions were not met, which in this case would be never. Maybe if you take them out of the first if ladder, maybe even out of the while loop (since they're printed at the end of the game) then you could get them to display properly.

    Might solve one of your problems.
    Last edited by Elhaz; 09-22-2004 at 10:18 AM.

Page 1 of 2 12 LastLast
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, 09:47 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  3. Criteria based random numbers
    By alkisclio in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2006, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21