Thread: Question about random repetition.

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    169

    Question about random repetition.

    How shall I put it,
    Are random functions (such as rand() or the ones boost offers) guaranteed to produce the same sequence of numbers on every machine, given that the function is seeded the same?
    Thank you!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is very likely, yes, if not guaranteed.
    Basically, they would compute mathematical formulas on the seed to get their "random" numbers.
    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.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Each implementation will give the same value for the same seed, I believe (so any VS2007 implementation should give the same values) -- or at least I'm not aware of any that differ, but it won't be the same from implementation to implementation.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    169
    This is just what I was hoping to find out.
    Thank you!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, to clarify:
    As long as it is the same source code and seed, yes. However, bear in mind that in most cases, your C runtime is a shared library (.dll or .so depending on the OS). This means that your application does not contain the actual implementation of the random number generation.

    You can (in most cases) make sure that the rand() or similar function is part of your application - this is called "static linking" - but there are drawbacks in the sense that you then get a much larger .exe, and any fixes to improve the C runtime that was compiled into your application will require a recompile - whilst the shared library build would automatically use the newer version if one is installed on the machine.

    If you really want to ENSURE this behaviour, implementing your own random number generator (based on known good algorithms - do not attempt to write your OWN algorithm for a random number generator) is probably the safest option. And of course, if you ever want portability between different compilers, different OS's or different C runtime libraries, you will almost certainly have to do your own code.

    For a small distribution base, you may simply do the "statically linked C runtime" and be done with it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    For where you need guaranteed same sequence across different implementations, I would just include a pseudo-random number generator in your code. Something like Mersenne Twister. The official site has a reference implementation, too, that is quite short and only requires <stdio>, and is under a liberal license.

    Code:
       Redistribution and use in source and binary forms, with or without
       modification, are permitted provided that the following conditions
       are met:
    
         1. Redistributions of source code must retain the above copyright
            notice, this list of conditions and the following disclaimer.
    
         2. Redistributions in binary form must reproduce the above copyright
            notice, this list of conditions and the following disclaimer in the
            documentation and/or other materials provided with the distribution.
    
         3. The names of its contributors may not be used to endorse or promote 
            products derived from this software without specific prior written 
            permission.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I'd say to grab one of George Marsaglia's implementations of one of the simpler algorithms and never look back.

    Soma

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    It's this simple -
    http://www.math.sci.hiroshima-u.ac.j...ES/mt19937ar.c

    Code:
    #include "mt19937.ar.c"
    
    int main() {
         init_genrand(12345);
         unsigned int random_number = genrand_int32();
    }

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by glo View Post
    How shall I put it,
    Are random functions (such as rand() or the ones boost offers) guaranteed to produce the same sequence of numbers on every machine, given that the function is seeded the same?
    Thank you!
    Its not a requirement, but realistic implimentations are deterministic. There are methods for producing truly random numbers, but they are non-trivial.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    There are methods for producing truly random numbers, but they are non-trivial.
    In addition, it can only be done with hardware support (collecting truly random noise from the nature), since computers are deterministic by nature.

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Well, software implimentations are never truly random, they are pseudo-random, and fully deterministic by nature.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  2. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM
  3. Noob question for random number
    By SKINp in forum C++ Programming
    Replies: 6
    Last Post: 01-03-2003, 12:53 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM