Can't understand FAQ

This is a discussion on Can't understand FAQ within the C++ Programming forums, part of the General Programming Boards category; what library and command do i need to get a random number between number x and number y? I am ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    8

    Can't understand FAQ

    what library and command do i need to get a random number between number x and number y? I am using a DEV C++ compiler if that makes anydifference. I looked in the FAQ and they dont address this directly at least not in the search results i found under random. thanks in advance.
    Last edited by Vorkosigan; 03-02-2004 at 08:16 PM.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Code:
    #include <cstdlib> //for rand() and srand()
    #include<ctime> //for time(0)
    
    int main()
    {
    srand(time(0)); //seed the random number generator
    ...
    int num=LOWER+rand()%(UPPER-(LOWER-1));
    ...
    }
    the LOWER and UPPER are just numbers you put it... they're the upper and lower limits... for example, num=1+rand()%6; will simulate a dice roll {1..6}

    EDIT: just remembered... the srand(time(0)) line will give you a warning... all you need to do to get rid of that warning is to cast the time(0) function to an unsigned int... your code will look like srand(static_cast<unsigned int>(time(0))); If you don't know what I'm talking about, don't bother with it for now...
    Last edited by major_small; 03-02-2004 at 08:58 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    i havent used dev c++ in a long time, but why do you have to static_cast it? why not just srand( (unsigned int) time(0) )?
    I came up with a cool phrase to put down here, but i forgot it...

Popular pages Recent additions subscribe to a feed

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