Generating Random Numbers
Some computing problems are surprisingly difficult, and creating good random
numbers is one of them. After all, one of the most important properties of a computer
is deterministic behavior. Still, we often need—at least seemingly—random behavior
for a variety of purposes, ranging from games to cryptographic algorithms.
The most popular approach (used in Arduino’s
random()
function) is to create pseudo-
random numbers.
a
They seem to be random, but they actually are the result of a
formula. Different kinds of algorithms exist, but usually each new pseudorandom
number is calculated from its predecessors. This implies that you need an initialization
value to create the first random number of the sequence. This initialization value is
called a random seed, and to create different sequences of pseudorandom numbers,
you have to use different random seeds.
Creating pseudorandom numbers is cheap, but if you know the algorithm and the
random seed, you can easily predict them. So, you shouldn’t use them for crypto-
graphic purposes.
In the real world, you can find countless random processes, and with the Arduino,
it’s easy to measure them to create real random numbers. Often it’s sufficient to read
some random noise from analog pin 0 and pass it as the random seed to the
random-
Seed()
function. You can also use this noise to create real random numbers; there is
even a library for that purpose.
b
If you need strong random numbers, the Arduino is a perfect device for creating them.
You can find many projects that observe natural processes solely to create random
numbers. One of them watches an hourglass using the Arduino.
c
a.
http://en.wikipedia.org/wiki/Pseudo-random_numbers
b.
http://code.google.com/p/tinkerit/wiki/TrueRandom
c.
http://www.circuitlake.com/usb-hourglass-sand-timer.html
Working with Buttons
In this section you’ll learn how pushbuttons work in principle and how you
can use them with an Arduino. We’ll start small and build a circuit that uses
a pushbutton to control a single LED.
What exactly is a pushbutton? The following figure shows three views of a
typical pushbutton. It has four connectors that fit perfectly on a breadboard
(at least after you have straightened them with a pair of pliers). Two opposite
pins connect when the button is pushed; otherwise, they are disconnected.
Chapter 3. Building Binary Dice • 48
report erratum • discuss
www.it-ebooks.info