Hardware Reference
In-Depth Information
To make sure you know what these random numbers look like, try this out at the
Python Shell:
1.
Click on the Python Shell window to bring it to the front.
2.
Import the random module so that you can use the built-in random function, by
typing:
import random
3.
Ask the program to generate a random number between 1 and 100 and print it
to the screen:
print(random.randint(1,100))
You should see a number between 1 and 100 appear on the screen. Type the
print
statement again. What number do you get this time?
4.
Now use a
for
loop to print lots of random numbers. Make sure you indent the
second line so that Python knows that the
print
statement is part of the
for
loop:
for n in range(50):
print(random.randint(1,100))
The two numbers inside the brackets of the
randint()
function tell it the range of
numbers you want it to generate; 1 is the smallest number you should expect it to gen-
erate, and 100 the largest.
A
random number
is usually generated from a random number sequence—a
list of numbers designed not to have any obvious pattern or repeating sequence.
Computers are very precise machines and often do not generate truly random
numbers; instead, they generate pseudo-random numbers. These numbers
might seem to be part of a random sequence, but there is a pattern to them. You
can read more about random numbers at
http://en.wikipedia.org/wiki/Random_
number_generation
and find out more about real random numbers at
www.
Laying the.Carpets
Earlier in this adventure you used an extra number in the
setBlocks()
function to
make the colour of the woollen carpet red, using the extra data of
WOOL
. The allowed
range of extra data numbers for
WOOL
is between 0 and 15—in other words, there are
16 colours you can choose from. Now use the following steps to change your house-
building program to generate a random number and use that as the colour of
WOOL
for
the carpet in the house: