I’ve always found the topic of random number generation more than just a little fascinating. Those who know me well, might not find that so much of a revelation.

I’ve been writing some programming materials this week which allowed me to further indulge my interest in random number generation in Python just a little more. I recommend reading this article on Wikipedia on random number generation , it makes a good place to start reading before reading any more here.

I often introduce the Python random module to classes I teach. In contrast to the logical, predictable nature of computing science, random always adds an air of excitement. I’ve even resorted to using it to help make decisions in class – for amusement and entertainment only of course.

One thing I have noticed is that children are sometimes not totally convinced how random the module actually is, and instead think that the module has been ‘fixed’ to generate results that favour their teacher. Try these lines in the interactive shell first.

>>> import random >>> print (random.randint(1,6))

When I’ve demonstrated this in class it can often repeat the same number more than once (said to be a characteristic of true randomness).

This afternoon, I decided to test out just how random the Python random module actually is. A development from this could be to display the result of 100 random numbers. So a count-controlled loop would accomplish this. If you are new to Python, open a new window and type in the following. Then save and run (press F5).

import random for a in range (1,100): n=random.randint(1,6) print(n)

A short piece of script like this only produces a visual way to check. A more mathematical approach would be to count the number of times individual numbers occur and then show the results.

So, my next development was to assign six different variables to count how many times there respective number was chosen. This next piece of script is rather crude, but I’m keeping it simple for the sake of clarity. One of the great things about computers is that they can do millions of calculations in seconds, so I also decided to boost the range to 1,000,000 to try more results. If you decide to print each of the individual dice throws, prepare to wait a while!

import random d1=0 d2=0 d3=0 d4=0 d5=0 d6=0 for a in range (1,1000000): n=random.randint(1,6) if n==1: d1=d1+1 if n==2: d2=d2+1 if n==3: d3=d3+1 if n==4: d4=d4+1 if n==5: d5=d5+1 if n==6: d6=d6+1 print (d1,d2,d3,d4,d5,d6)

Well, the results appear to show that each time I ran the script there was a fairly even distribution of numbers. I suppose if I was to show this to a class, I might ask them to devise their own test first. There would be no fun or challenge in me showing them how I solved the problem first. I have not tried this with a class yet, but when I do, I’m sure I will ask them to predict out of a 100 rolls of the dice how many times any specific number should appear. You would expect somewhere around 16 times. So the next challenge would be to ask them to create something to test out their theory.

A much simpler version would be to test for a specific number:

import random d1=0 for a in range (1,100): n=random.randint(1,6) if n==1: d1=d1+1 print (d1)

Try it with a class and let me know how you get on.

I’ll be at PyConUK from 20-23 September working with teachers and developers to create resources for teachers and pupils to share. You can find out more about PyConUK here and I would encourage you to attend. We’re holding a great big Raspberry Jam on the Sunday.

Using the random number generator to make decisions in class? There’s lazy teacher software that kicks up random students’ names so calling on individuals is fairer. This would be an excellent programming exercise!

LikeLike