Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Quick C++ Question

  • 29-11-2005 12:57PM
    #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,544 ✭✭✭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