Pages

Wednesday, June 1, 2016

Computer random number generators (RNGs) [1] have many uses. In a programming class I was teaching recently, a participant was surprised by a certain usage of random numbers that I showed; which I thought would be common knowledge. That made me realize that many novices, and possibly even some more experienced programmers, may not be aware of some among the many useful applications of random numbers.

That gave me the idea for this post, in which I'll show some of the ways in which random numbers are useful. The examples are in Python, but the concepts and techniques can be applied in any programming language that
has a random number generation facility.

[1] Strictly speaking, these are really pseudo-random number generators (PRNGs), but I'll call them RNGs for short.

We can see that the module has many functions and other attributes, such as the important mathematical constants e (_e) and pi (_pi). (Those last two are also available in the math module as e and pi.)

In this post, I'll look at some uses of one of the most fundamental functions in the module, called random(), like the module. In a following post or two, I'll look at other functions, and also other uses in different areas, including some less obvious ones.

First print its docstring:

>>>print random.random.__doc__
random() -> x in the interval [0, 1).

It returns a random float value in the half-closed interval [0, 1), which means, any x, such that 0.0 <= x < 1.0.

Here is a program, fn_random.py, that shows some uses of the random() function:

It shows how to scale and offset the values you get from random(), to transform them from the range of 0 to 1, to other ranges.

Notice that the values of the random numbers in the four sets of output are all different, even if you take the scaling into account. For example, the numbers in the second set of output are not the same as the numbers in the first set multiplied by 10, even though that is what we do in code for the second set. The reason for this is that the random numbers generated, cycle through a very long sequence, and so the first 10 numbers are output in set 1, the second 10 in set 2, and so on.

What if we wanted to have the same values of random numbers (except for the differences caused by scaling and offsetting) in all 4 output sets, say for the sake of some sort of consistency or repeatability in some scientific or statistical experiment? One obvious way is to save the first 10 numbers generated in the first snippet (say in a list) and use them in the following 3 snippets.

There is another way to do it, with the getstate() and setstate() functions of the module.

Put this line:

state = random.getstate()

before the snippet (Ex. 1.) that generates the first set of output.

Then put this line:

random.setstate(state)

before each of the following three snippets (Ex. 2 to 4.). That will do it, since now the same 10 random numbers will be generated by each snippet (before taking into account the scaling and offsetting).

In the next post, I'll show some other uses of random numbers, such for doing things with strings.