Thread: A few questions

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    A few questions

    Hi the first question is, how can I set a min and max for the rand function. At the moment i'm seeding from ctime then using
    Code:
    int theNumber = rand() % max + 1
    It picks a number between 0 and 100 then if its too high it sets the max integer to the number just guessed. How can I change the min one.

    My second question is:

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        string input;
        const int MAX_ITEMS = 10;
        int inventoryCount = 0;
        string inventory[MAX_ITEMS];
        vector<string> shopList;
        int shopListCount = 0;
        shopList.push_back("Sword") && (shopListCount++);
        shopList.push_back("Shield" && (shopListCount++);
        shopList.push_back("Firewood") && (shopListCount++);
        
        cout << "Welcome to Ben's Test RPG, input inventory if you want to see what you own, type quit to leave aand type shop to see the shop" << endl;
        
        
        do
        {
             cin >> input;
             
             if(input == "inventory")
                      
                      if(inventoryCount != 0)
                                        
                                        for (int i=0; i < inventoryCount; i++)
                                            
                                            cout << inventory[i];
                                        
                      else
                                        
                                        cout << "Sorry you have no items." << endl;
                                        
             
             if(input == "shop")
             
                      for (int j=0; j < shopListCount; j++)
                          
                          cout << shopList[j];
                      
             if(input == "quit")
             ;
             
             else
             
                 cout << "Sorry incorrect choice.";
                 
                 }while(input != "quit")
                 
                 }
    How can I make it so it adds to my shopListCount when the vector gets pushed back.

    Thanks,
    Ben

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Chipmunkey View Post
    It picks a number between 0 and 100 then if its too high it sets the max integer to the number just guessed. How can I change the min one.
    Imagine you could generate a random number between 0 and 50. Now add 50 to this number. You now have a random number between 50 and 100. Make sense?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    how can I set a min and max for the rand function
    Read Prelude's article on Using rand().

    It picks a number between 0 and 100 then if its too high it sets the max integer to the number just guessed.
    Your code snippet produces a number between 1 and max inclusive.

    How can I make it so it adds to my shopListCount when the vector gets pushed back.
    Just increment:
    Code:
    shopList.push_back("Sword");
    ++shopListCount;
    shopList.push_back("Shield");
    ++shopListCount;
    shopList.push_back("Firewood");
    ++shopListCount;
    Of course, since you are manually inserting 3 items, you might as well initialize shopListCount to 3.
    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

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why do you need shopListCount at all? Just use shopList.size().

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    shopList.push_back("Shield") && (shopListCount++);

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Finally... A practical use for high school algebra.

    Well we know that rand() % num gives us a number from 0 to (num - 1). So after a few revisions and tests in my head, here's my final answer:
    (rand() % (max - (min - 1))) + min

    If min = 50 & max = 100 then:

    (rand() % (100 - (50 - 1))) + 50
    =(rand() % (100 - 49)) + 50
    =(rand() % 51) + 50
    =(0...50) + 50
    =(50...100)

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. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM