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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Quick C++ Question

  • 29-11-2005 11:57am
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 7,516 ✭✭✭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, Registered Users 2 Posts: 871 ✭✭✭gerTheGreat


    I'll give it a shot

    thanks
    Ger...


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


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


Advertisement