Thread: Pseudorandom Number Generator

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    45

    Pseudorandom Number Generator

    As far as I know, there is no pseudo random number generator for c. I don't know much about probability and statistics, but does anyone else know of a method or function (or perhaps a library that contains a function) that returns a pseudo random scalar value drawn from a normal distribution with mean 0 and standard deviation 1?

    I was able to find some code on the internet, but it uses the rand() function, which I heard is not all that random in c.
    Here's the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    /* Generate a normal random variable with mean 0 and 
    
    standard deviation
    of 1. To adjust to some other distribution, multiply by the 
    
    standard
    deviation and add the mean. Box-Muller method
    note: rand() is a function that returns a uniformly 
    
    distributed random
    number from 0 to RAND_MAX
    */
    double randn()
    {
    static double V2, fac;
    static int phase = 0;
    double S, Z, U1, U2, V1;
    
        if (phase)
            Z = V2 * fac;
       else
       {
           do {
               U1 = (double)rand()/ RAND_MAX;
               U2 = (double)rand() / RAND_MAX;
    
    
               V1 = 2 * U1 - 1;
               V2 = 2 * U2 - 1;
               S = V1 * V1 + V2 * V2;
           } while(S >= 1);
    
    
           fac = sqrt (-2 * log(S) / S);
           Z = V1 * fac;
       }
    
    
        phase = 1 - phase;
    
    
        return Z;
    }
    Any help would be greatly appreciated.

    Thank you.
    Last edited by magda3227; 07-16-2008 at 10:46 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by magda3227 View Post
    As far as I know, there is no pseudo random number generator for c. I don't know much about probability and statistics, but does anyone else know of a method or function (or perhaps a library that contains a function) that returns a pseudo random scalar value drawn from a normal distribution with mean 0 and standard deviation 1?

    I was able to find some code on the internet, but it uses the rand() function, which I heard is not all that random in c.
    How can there be no prng for C when you've just mentioned it - rand. rand is nowhere near as bad as some people think. For almost all purposes the results are indistinguishable from anything better, assuming you use it properly, i.e. not calling srand each time etc.
    Switching from rand to something else without working out that rand isn't good enough is premature pessimation/optimisation (you choose).
    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"

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Except the implementation of rand() isn't specified. Today with your current compiler, it's pretty good. Tomorrow with code-musher 9.2, it's rubbish.

    rand() is good for student dice rolling problems, and little else.

    If you're doing serious statistical work (or crypto work), you need a better source you can be sure of.
    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.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Salem View Post
    Except the implementation of rand() isn't specified. Today with your current compiler, it's pretty good. Tomorrow with code-musher 9.2, it's rubbish.
    People probably shouldn't be put off just because it's possible that there might be some rubbish implementation out there somewhere. Implementations of common compilers such as MSVC++ are "pretty good", and that all that matters to most people.

    rand() is good for student dice rolling problems, and little else.
    This is exactly the kind of broad over-generalisation I was referring to. The number of applications rand is perfectly suited to is far larger than that. For almost anything random in most games ever made, rand should do (except where knowing the prng would make it possible to cheat). I mean no human is able to tell the difference between a tic-tac-toe program using rand and one using something better.
    The whole "real programmers use mersenne twister every time" notion that people occasionally like to boast, gets rather tiresome. It's purely premature optimisation.
    People should learn that rand is far from a silver bullet as far as random number generators are concerned though.

    If you're doing serious statistical work (or crypto work), you need a better source you can be sure of.
    I can certainly agree with that. Hell I'd certainly prefer CryptGenRandom in such cases. It's all about knowing what tool is best for the job.

    Okay this thread may be about something statistical, so perhaps mersenne twister would be appropriate.
    Last edited by iMalc; 07-17-2008 at 01:19 AM.
    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"

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > For almost anything random in most games ever made, rand should do
    In other words, programs which have no consequence of having a bad PRNG can get away with using rand().

    What does "pretty good" mean anyway?
    Good enough for "joe public" to think it's random?

    If your livelyhood is online games of chance, then "pretty good" just won't cut it if there's any chance that people might figure out what the PRNG is and start predicting the results in advance.
    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.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Salem View Post
    > For almost anything random in most games ever made, rand should do
    In other words, programs which have no consequence of having a bad PRNG can get away with using rand().
    Actually I think that probably is a good description of when one can use rand, and your other statements are fine with me also.
    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. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  2. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  3. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM
  4. Good Random Number Generator
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 11-18-2004, 06:38 AM
  5. Replies: 2
    Last Post: 01-04-2004, 05:52 PM

Tags for this Thread