Can rand() really be this bad?
c++, macos, random
Solution
Have you tried `arc4random()`? It provides much stronger and more uniform random values than `rand()`.
You could also try `SecRandomCopyBytes()`, which is part of Security.framework. This basically just reads from /dev/random.
Problem
Above is an example image generated by using rand() to get random coordinates and add a constant to the pixel value at those coordinates. This is how it looks from a few thousand iterations. I'm using the rand() from stdlib.h in Mac OS X Lion, giving it time(NULL) as the seed. You can clearly see the vertical lines, as if the ones with an odd x-coordinate have higher values than those with even x-coordinates. How would I implement a better algorithm, or where can I find one that doesn't have many dependencies? (I would prefer a header-only file). Here's the code (sorry it took me so long): ``` void generate(int iterations = 1) { for (unsigned int x = 0;x < (area * 4);++x) { map[rand() % area] += 1; } number a = min(); number b = max(); for (int i = 0;i < area;++i) { map[i] -= a; map[i] /= b; } } ``` Map contains double-floats and is later turned into RGB values.