Thread: Random numbers in C++

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    Random numbers in C++

    I noticed that in BASIC, if you used the RND function get an array of say, 10 characters, it would randomly fill an array with numbers. However, if you ran the program again, those same numbers would always show up. Does this happen in C++. I don't know what the function is, but I can find that pretty easily, so don't worry about that. Thanks if you can help.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The random number depends on which seed the random generator gets. Using time(NULL) which returns the number of seconds since a certain date, you always get a unique number.
    You set it up with srand(time(NULL)), and then get a random number using rand() % n (a number in the interval 0 to (n-1))
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

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

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    Post

    Alrighty - thanks alot. Hammer - yeah I know - it's a habit I should lose - I'm still used to the old boards - when they only had like 5 or 6 forums.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM