Showing posts with label RNGs. Show all posts
Showing posts with label RNGs. Show all posts

Monday, July 11, 2016

The many uses of randomness - Part 2

By Vasudev Ram


Denarius image attribution

Hi, readers,

In my previous (and first) post on randomness, titled:

The many uses of randomness ,

I had described some uses of random numbers related to floats, and ended by saying I would continue in the next post, with other uses, such as for strings (and things).

This is that next post (delayed some, sorry about that).

Assume that the following statements have been executed first in your Python program or in your Python shell:
from __future__ import print_function
import string
from random import random, randint, randrange, choice, shuffle
Let's look now at the use of random numbers to generate random character and string data.

First, let's generate a few different kinds of random characters:

1) Random characters from the range of 7-bit ASCII characters, i.e. the characters with ASCII codes 0 to 127. This expression generates a single ASCII character:
chr(randint(0, 127))
Each time the above expression is evaluated, it will generate a random character whose code is between 0 and 127.

As a result, it may sometimes generate non-printable characters, such as the characters with codes in the range 0 to 31, and 127. See the Wikipedia article about ASCII above, for information on printable versus non-printable characters.

To generate only printable ASCII characters, use:
choice(string.printable)

We may want to generate all ASCII characters, or even all printable characters, only for some specialized purposes. More commonly, we may want to generate printable random characters from a specific subset of the complete ASCII character set. Some examples of this would be: generating random uppercase letters, random lowercase letters, random numeric digits, or combinations of those. Here are a few code snippets for those cases:
# Generate random uppercase letter.
chr(randint(ord('A'), ord('Z')))
(which relies on the fact that the ASCII codes for the characters 'A' through 'Z' are contiguous).
Or, another way:
# Generate random uppercase letter.
choice(string.ascii_uppercase)
# -------------------------------------------
# Generate random lowercase letter.
chr(randint(ord('a'), ord('z')))
Or, another way:
# Generate random lowercase letter.
choice(string.ascii_lowercase)
Random numbers can be used to generate random strings, where the randomness of the strings can be in either or both of two dimensions, the content or the length:

Generate strings with random character content but fixed length, e.g.: "tdczs", "ohybi", "qhmyf", "elazk"
def rand_lcase_str(n):
    '''Return string of n random lowercase letters.'''
    assert n > 0
    rand_chars = [ choice(string.ascii_lowercase) for i in range(n) ]
    return ''.join(rand_chars)

# Calls and output:
[ rand_lcase_str(3) for i in range(1, 8) ]
['xio', 'qsc', 'omt', 'fnn', 'ezz', 'get', 'frs']
[ rand_lcase_str(7) for i in range(1, 4) ]
['hazrdwu', 'sfvvxno', 'djmhxri']

Generate strings with fixed character content but random lengths, e.g.: "g", "gggg", "gg", "ggggg", "ggg"; all strings contain only letter g's, but are of different lengths.
def rand_len_fixed_char_str(c, low_len=1, high_len=256):
    '''Return a string containing a number of characters c,
    varying randomly in length between low_len and high_len'''
    assert len(c) == 1
    assert 0 < low_len <= high_len
    rand_chars = c * randint(low_len, high_len)
    return rand_chars

# Calls and output:
[ rand_len_fixed_char_str('g', 3, 8) for i in range(10) ]
['gggg',
 'ggggggg',
 'ggg',
 'ggggggg',
 'ggggg',
 'ggggg',
 'gggggg',
 'gggggg',
 'gggggg',
 'ggggg']
Generate strings with both random character content and random lengths, e.g.: "phze", "ysqhdty", "mltstwdg", "bnr", "q", "ifgcvgrey". This should be easy after the above snippets, since we can use parts of the logic from some of them, so is left as an exercise for the reader.

Such kinds of randomly generated data are useful for many purposes, e.g. for testing apps that read or write CSV or TSV files, fixed-length or variable-length records, spreadsheets, databases; for testing report generation logic (particularly with respect to text formatting, wrapping, centering, justification, logic related to column and line widths, etc.).

All these use cases can benefit from running them on random data (maybe with some programmed constraints, as I showed above), to more thoroughly test the app than can be done manually by typing in, say, only a few dozen variations of test data. There are at least two benefits here:

- a program can be more systematically random (if that makes sense) than a human can, thereby giving test data that provides better coverage;

- the computer can generate large volumes of random data for testing the app, much faster than a human can. It can also feed it as input to the software you want to test, faster than a human can, e.g. by reading it from a file instead of a user typing it. So overall, (parts of) your testing work can get done a lot faster.

In the next part, I'll show how, using a mathematical concept, random numbers can be used to reduce the amount of test data needed to test some apps, while still maintaining a good level of quality of testing. I will also discuss / show some other uses of randomness, such as in web development, and simulating physical events.

The image at the top of the post is of a Roman denarius in silver of Maximinus (235-238). The word denarius seems to be the origin of the word for money in multiple modern languages, according to the linked article.

- Vasudev Ram - Online Python training and consulting

Signup to hear about my new courses and products.

My Python posts     Subscribe to my blog by email

My ActiveState recipes



Wednesday, June 1, 2016

The many uses of randomness

By Vasudev Ram


Dice image attribution




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.

(The Wikipedia article on random number generation has information on the difference between RNGs and PRNGs - and a lot more interesting information on random numbers in general, including their use in ancient times, e.g. the use of yarrow stalks (for divination) in the I Ching :)

Note: None of the random number uses that I will show requires advanced mathematical knowledge.

Let's start.

Import and introspect the random module from Python's standard library:
$ python
>>> import random
>>> dir(random)
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'System
Random', 'TWOPI', 'WichmannHill', '_BuiltinMethodType', '_MethodType', '__all__'
, '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_acos', '_c
eil', '_cos', '_e', '_exp', '_hashlib', '_hexlify', '_inst', '_log', '_pi', '_ra
ndom', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betava
riate', 'choice', 'division', 'expovariate', 'gammavariate', 'gauss', 'getrandbi
ts', 'getstate', 'jumpahead', 'lognormvariate', 'normalvariate', 'paretovariate'
, 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'tr
iangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
>>>
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:
from __future__ import print_function

# fn_random.py
# A program showing various uses of the "random" function 
# from the "random" module of Python's standard library.
# Author: Vasudev Ram - https://vasudevram.github.io
# Copyright 2016 Vasudev Ram

from random import random
from random import getstate, setstate

print("Ex. 1. Plain calls to random():")
print("Gives 10 random float values in the interval [0, 1).")
for i in range(10):
    print(random())

print()

print("Ex. 2. Calls to random() scaled by 10:")
print("Gives 10 random float values in the interval [0, 10).")
for i in range(10):
    print(10.0 * random())

print()

print("Ex. 3. Calls to random() scaled by 10 and offset by -5:")
print("Gives 10 random float values in the interval [-5, 5).")
for i in range(10):
    print(10.0 * random() - 5.0)

print()

print("Ex. 4. Calls to random() scaled by 20 and offset by 40:")
print("Gives 10 random float values in the interval [40, 60).")
for i in range(10):
    print(20.0 * random() + 40.0)
Here is the program's output when run with:
python fn_random.py
Ex. 1. Plain calls to random():
Gives 10 random float values in the interval [0, 1).
0.978618769308
0.429672807728
0.807873374428
0.00775248310523
0.367435959496
0.452718276649
0.15952248582
0.183989787263
0.240112681717
0.873556193781

Ex. 2. Calls to random() scaled by 10:
Gives 10 random float values in the interval [0, 10).
8.66851830984
8.06203422551
6.68791916223
6.83023335837
2.28298961244
5.06491614858
9.27404238781
2.30473573581
7.62983863372
7.18179372151

Ex. 3. Calls to random() scaled by 10 and offset by -5:
Gives 10 random float values in the interval [-5, 5).
4.28960887925
-1.48913007202
-0.534170376124
3.36741617894
-3.50802287142
1.46218930484
-3.37237288568
-2.49675571636
-3.56593768859
-1.49924682779

Ex. 4. Calls to random() scaled by 20 and offset by 40:
Gives 10 random float values in the interval [40, 60).
56.4292882009
40.888150119
56.867782498
58.3162130934
41.642982556
40.8419833357
52.3684662857
45.3000297458
43.1515262997
52.1129658036
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.


See also:

Pseudo-random number generator
Hardware random number_generator


The picture below, from Wikipedia, is the title page of a Song dynasty (c. 1100) edition of the I Ching.

I Ching image attribution

print ['Enjoy', 'See you soon', 'Bye for now'][randint(0, 2)] + " :)"

- Vasudev Ram - Online Python training and consulting

Signup to hear about my new courses and products.

My Python posts     Subscribe to my blog by email

My ActiveState recipes