How to get a random number

This is a discussion on How to get a random number within the C++ Programming forums, part of the General Programming Boards category; What would you do to get a random number from say, 1 to 5? I know the function is called ...

  1. #1
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209

    How to get a random number

    What would you do to get a random number from say, 1 to 5? I know the function is called rand(), but I don't know how to use it.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Ignoring all of the sticky issues that can come up:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      for (int i = 0; i < 10; i++)
        cout<< rand() % 5 <<endl;
    }
    You'll run into the sticky issues in the following order:
    1. The same random sequence each time the program is run (use srand)
    2. The numbers may not be that random (don't use modulus)
    3. rand sucks (get a better random number generator)

    Conveniently enough, the first two are the most common, and they're answered in the FAQ. The last is a problem that you'll only run into when you're good and ready to tackle it, so no worries at this point.
    My best code is written with the delete key.

  3. #3
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Alright, thanks.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    shouldn't you be using
    Code:
    1+rand()%5;
    to get a range of 1..5?
    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

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >shouldn't you be using
    I looked again at the first post, and sure enough it said 1 to 5. How I misinterpreted that as "up to 5" is beyond me.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 03:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 01:56 PM
  4. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 03:41 PM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 08:34 PM

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