Thread: the algorithm used in c++ to generate random numbers

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    the algorithm used in c++ to generate random numbers

    Hi,
    could you please give me a hint about the algorithm that c++ uses when we use rand () command.
    there are many algorithm. but most simple one is:Linear Congruential Generator(LCG)

    does c++ use (LCG) when we use rand () ??

    thanks for your help

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    26
    I don't think the standard defines rand()'s algorithm.

    But have a look here(C++11):
    Pseudo-random number generation - Cppreference

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Why not find it out yourself?

    Write a program that uses rand() and step into it with a debugger.

    My compiler uses:

    Code:
    return( ((ptd->_holdrand = ptd->_holdrand * 214013L + 2531011L) >> 16) & 0x7fff );
    Yours might differ.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The standard does not mandate what algorithm is used to implement functions, but rather its requirements, what input it must take and what it must return. It is up to the compiler vendor to implement that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    thanks for your help

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    The standard does not mandate what algorithm is used to implement functions, but rather its requirements, what input it must take and what it must return. It is up to the compiler vendor to implement that.
    Requirements are about more than inputs and outputs. Some requirements are about performance (or, more accurately, algorithmic complexity and space complexity).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is what I meant by requirements. You just stated it a little more detailed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User kaspari22's Avatar
    Join Date
    Jul 2008
    Location
    Czech Republic, Doubravice
    Posts
    14
    I think srand is what you looking for... check cplusplus reference

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to generate random numbers the same everytime
    By lehe in forum C++ Programming
    Replies: 11
    Last Post: 04-09-2009, 06:00 PM
  2. How to generate random numbers between 65 and 75
    By chottachatri in forum C Programming
    Replies: 19
    Last Post: 03-02-2008, 06:24 PM
  3. Generate Random Numbers and Assign to Days of Week
    By mms in forum C++ Programming
    Replies: 10
    Last Post: 05-04-2006, 01:51 AM
  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. Replies: 10
    Last Post: 11-23-2001, 10:01 AM