Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Quick C++ Question

Options
  • 29-11-2005 12:57pm
    #1
    Registered Users Posts: 871 ✭✭✭


    I was just wondering if it is possible to specify parameters for the rand() function, eg between 1 and 100.

    Any ideas would be greatly appreciated
    Thanks
    Ger...


Comments

  • Registered Users Posts: 7,498 ✭✭✭BrokenArrows


    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    using std::cout;
    using std::endl;
    
    int main()
    {
    
        int j;
        const int N = 100;
    
        // Set evil seed (initial seed)
        srand( (unsigned)time( NULL ) );
    
        for (int i = 0; i < 5; i++) {
            j = (int) N * rand() / (RAND_MAX);
            cout << j << endl;
        }
    
        return 0;
    }
    


    This code should work, havent tried it but found it here
    http://cplus.about.com/od/advancedtutorials/l/aa041303c.htm


  • Registered Users Posts: 871 ✭✭✭gerTheGreat


    I'll give it a shot

    thanks
    Ger...


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    Or just use
    rand() % max;
    
    to get a number between 0 and max-1 inclusive.


Advertisement