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

2. Originally Posted by magda3227
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).

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

4. Originally Posted by Salem
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.

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

6. Originally Posted by Salem
> 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.