Possible Random

The obvious: Random

openFrameworks offers a function called ofRandom. It allows the generation of a complete random float value between two given numbers.

ofRandom(25); // could give you 14.7587. Or 5.4464. Or 0.0001
ofRandom(10, 11); // would maybe be 10.0032, 10.9544, …

Having a look into an ongoing discussion on GitHub, the result of the first example can be up to 24.9999, but can’t be 25 itself.

There are also some shorthands for the ofRandom function:

ofRandomf(); // generates a random float between -1 and 1
ofRandomuf(); // generates a random float between 0 and 1

Extra: Random direction, 1 or -1?

int direction = (round(ofRandomuf())*2)-1;

The variable direction now either holds 1 or -1.

The natural: Perlin Noise

Perlin noise was developed by Ken Perlin for the 1982 film Tron. It is used to generate smooth, natural appearing textures. Daniel Shiffman did a great introduction video on this algorithm as part of his Nature of Code series.

The return value of the function will be a float between 0 on 1.

t += 0.01;
ofNoise(t); // would give you something like 0.700621, 0.70035, 0.699634, 0.698481, …

Additionally, to generate noise signed values between -1 and 0, the function ofSignedNoise() can be used.

The missing: Gaussian Random Numbers

As until now, the openFrameworks core does not support the generation of gaussian numbers. Luckily, there are several attempts to solve this issue. I had no chance to test any of it myself, but the addon called ofxGaussian by Andy Lambert looks quite promising.

Tested with openFrameworks 0.8.0


Date: 2013-09-02 | Tags: openFrameworks, Random