Thread: Random number generation

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    Random number generation

    Hi - I'm not sure if this is "specific to C," but I just had a question for all the C/C++ programmers out there.

    Currently, in my programs, I generate random numbers using srand() and rand() per usual. However, I am wondering about how good of a random number generator rand() really is. I am beginning to program scientific simulations (thin film nucleation) and I am always a bit nervous about the quality of the results I get.

    Is there an accessible, implementable and better random number generator out there? A library or something? Or is rand() good enough? Let me hear your opinions.

    Thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    rand() generates pseudo-random numbers. The numbers it generates are pretty good, but if you want better random numbers, you could use random() or urandom() if you're on Linux. I'm sure there are other functions in other libraries out there too. (random.org comes to mind.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It depends on the implementation and your exact requirements, and the C standard does not mandate any particular implementation. The Mersenne Twister is a well known algorithm that you can use, with an implementation available from Prelude's website, but it certainly will not do for a cryptographic PRNG (which you do not seem to require).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The GNU scientific library has a fair number of good PRNGs.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To start from the back, yes, there are other random number generators available. The Mersenne Twister is a good candidate.

    However, the question we have to clarify is how you would classify "good" in a random number generator versus a bad one. There are several qualities:
    1. repeat distance - how many numbers do you need to take out before you get the same sequence of numbers back again?
    2. spread/variation - if you get a high number, how likely is it that the next number is also high, or much lower - there should be no "pattern" to this.
    3. Low vs. high bit variability - Do all parts of the number "vary" the equally, or are for example the
    4. Predictability - If we know what the last few numbers (say 5) are, can we predict what the next number should be?

    Of course, some of those variables will be more important than others for a particular system. An encryption mechanism would be more concerned with repeat distance and predictability than spread/variation. Note also the a long repeat distance doesn't necessarily mean a good randomness - just counting a 32-bit number up by 1 each time will get a 4 billion repeat distance, but very predictable.

    There is a lot of research on various randum number generators...

    --
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Additionally, you may not want the numbers to be evenly distributed, e.g. you may want to have a gaussian distribution (normal/bell-shape distribution) where 50% of samples are in the middle 30% of the range, and much less than average likelyhood to hit the outer 10% of the range.

    There are other ways to distribute numbers in a range too.

    rand is "even" in it's distribution, that is it's equally likely that you get a number anywhere in the range [or at least, it should be if it's a decent implementation].

    --
    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.

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    The best attempt I've ever seen at a random number generator:
    Code:
    int getRandomNumber() 
    {
        return 4; /* randomly chosen by throw of a dice */
    }


    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    2
    Well, I am not interested in a PRNG for crytographic purposes. Of the 4 criteria matsp listed, spread/variation is most important to me.

    I am not sure if a uniform or normal distribution would be better for my purposes - but I would like to try a PRNG with a normal distribution, if there is one readily available. (Does Mersenne twister have normal distribution?)

    I am so behind, I don't really have a way of clearly articulating what I'm looking for. My simulations with rand() are reproducing model results just fine, but I want to be sure that I'm not overlooking any significant "hidden" biases or issues stemming from use of rand(). I really don't know anything about rand() - but somehow, I don't think that other, mainstream, "scientific" simulations of interface growth are using rand() for random number generation.

    Is rand() good enough, would any of you say? Does it suit you for your purposes?

    Sorry for not having a more well-posed problem. I am looking into all the links/suggestions that have been offered so far and I greatly appreciate them.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want to try a normal distribution PRNG, there's one in the GNU library, and there's one in Boost/TR1 (at least I'm pretty sure it's in TR1).

    That said, you have to ask yourself what your random number is supposed to mean in the context of thin film nucleation -- is it supposed to be normal? Uniform? Etc.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Despite some of the pulicity, I don't believe rand() is BAD. It is not great either, but for most intents and purposes, and given a good seed (such as time with good spread - don't expect it to give you good variation if you run the same program 60 times in the same minute).

    Obviously, as tabstop says, you need to understand what the distribution is like in reality when using random numbers for simulations - it would be rather silly to have a uniform distribution to simulate for example human height - the distribution should be "normal". On the other hand, distribution of throwing a dice should be uniform, because 1, 2, or 6 is equally likely to happen.

    I found this:
    http://www.dreamincode.net/code/snippet1446.htm

    Not sure how useful it is.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  3. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 05:23 PM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  5. Random Number Generation
    By drdroid in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 08-02-2003, 03:35 AM