Like Tree1Likes
  • 1 Post By Salem

Random function @ stdlib.h: how do they behave?

This is a discussion on Random function @ stdlib.h: how do they behave? within the C Programming forums, part of the General Programming Boards category; Hi all, When generating pseudorandom numbers using the ISO C library stdlib.h (the classical sequence): Code: srand(mySeed); int myRandomValue = ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Brasília, Brazil
    Posts
    10

    Lightbulb Random function @ stdlib.h: how do they behave?

    Hi all,

    When generating pseudorandom numbers using the ISO C library stdlib.h (the classical sequence):
    Code:
    srand(mySeed);
    int myRandomValue = rand();
    I need to know what effectively happens. As far as I know, the system calculates a pseudorandom number using a given equation, envolving the RAND_MAX value, defined at the same stdlib.h library.

    That is important to me because I intend to use the same code on different platforms (including the well-known MinGW @ i386), so I expect to get the same random numbers on different enviroments.

    Any clue?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,328
    Quote Originally Posted by artur
    I need to know what effectively happens. As far as I know, the system calculates a pseudorandom number using a given equation, envolving the RAND_MAX value, defined at the same stdlib.h library.
    The implementation of srand and rand is implementation defined.

    Quote Originally Posted by artur
    That is important to me because I intend to use the same code on different platforms (including the well-known MinGW @ i386), so I expect to get the same random numbers on different enviroments.

    Any clue?
    Use a known PRNG instead of using rand.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Brasília, Brazil
    Posts
    10
    Quote Originally Posted by laserlight View Post
    The implementation of srand and rand is implementation defined.


    Use a known PRNG instead of using rand.
    Really? So I'll need a good PRNG. By mencioning it, you meant that I'd have to implement my own PRNG code?

    Thanks for the fast answer!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,328
    Quote Originally Posted by artur
    you meant that I'd have to implement my own PRNG code?
    No, you could use an existing library. For example, search the Web for "Mersenne Twister".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > Really? So I'll need a good PRNG. By mencioning it, you meant that I'd have to implement my own PRNG code?
    What do you want it for?

    stdlib.h rand() is good for student homework only

    The main users of randomness are "statistics and simulations" and "cryptography". If you've got a good hardware RNG that can deliver as much randomness as you need on demand, then use that.

    But if you're stuck with needing a pseudo RNG, then use Mersenne Twister (there are pre-written libs available) for all your non-crypto work.

    crypto work is much more demanding.
    A hardware RNG has to be the preferred option.
    If you're stuck with some kind of software solution, then perhaps read the following
    /dev/random - Wikipedia, the free encyclopedia
    Blum Blum Shub - Wikipedia, the free encyclopedia
    But beware that any s/w solution has issues and edge cases which might make things less than straightforward (not that any crypto is straightforward).
    rags_to_riches likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Some people take a very narrow view on what a basic PRNG is useful for. It's neither as bad as some people say, nor as good as some people think. Provided you use it properly, and for the right things, it works well.

    The implementation used in Visual Studio is:
    Code:
    static unsigned int g_seed;
    inline void srand(int seed)
    {
      g_seed = seed;
    }
    inline int rand()
    {
      g_seed = (214013*g_seed+2531011);
      return (g_seed>>16) & 0x7FFF;
    }
    If you just copy and use that in your project, along with your own definition of something like RAND_MAX, then it will clearly be the same on every system you use it on.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ objects behave upon assignment
    By derder in forum Windows Programming
    Replies: 1
    Last Post: 11-07-2011, 04:10 AM
  2. SetTimer() doesn't behave.
    By OnionKnight in forum Windows Programming
    Replies: 9
    Last Post: 04-09-2007, 03:52 PM
  3. Why does it behave so
    By mudigonda_ms in forum C Programming
    Replies: 6
    Last Post: 12-21-2003, 12:54 AM
  4. Two Struct & Class Programs that behave the same
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2002, 09:59 AM
  5. Why the function behave like that?
    By zahid in forum C Programming
    Replies: 1
    Last Post: 12-21-2001, 05:56 AM

Tags for this Thread


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