Mapping the random function

As described over here, the rand() function in C++ is a pseudo random generator that returns a number between 0 and RAND_MAX. This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using srand.

RAND_MAX is a constant defined in <cstdlib>

But just how random is the rand() function. Its easy to find out by calling the function and comparing how often different values are returned. I do this graphically using OpenGL code to render the graph.

Using fraps, I recorded the variation of outputs from the rand() function.

The graph tends towards a straight line indicating that the more often you call the rand() function, the more even its distribution.

Advertisements
Previous Post
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: