A few questions

This is a discussion on A few questions within the C++ Programming forums, part of the General Programming Boards category; Hi the first question is, how can I set a min and max for the rand function. At the moment ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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
    22,304
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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, 09: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, 06: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, 08: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, 08:22 AM

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