Random Gen
Random Gen
a3 a2 a1 a0
b3 b2 b1 b0
(8) (4) (2) (1)
The m-sequence
• We start off by “seeding” the circuit with all
ones on the outputs of the single-bit
memories.
• The input of the exclusive-or gate has two
ones on it, so the output is zero.
• The output of the circuit is 1+2+4+8 = 15
• On the next clock pulse, inputs become
outputs. Output b3 becomes zero. Circuit
output becomes 1+2+4+0 = 7
The m-sequence
• Easy exercise:
• Please work out the 15 numbers in the
sequence, until it starts to repeat.
• Then work out what would happen if we
stupidly seed it with all zeroes and start it
off.
• (5 mins)
The m-sequence
• Now you know what we’re talking about,
let’s talk about it....
• It goes through every possible number
except 0.
• The sequence is (2n -1) long. Here we
have 4 shift-registers, 24-1 = 15. So with
only 16 shift registers we can have a
sequence of 216-1 = 65535. It’s extremely
efficient
The m-sequence
• Balance property: All numbers (except
zero) occur only once.
• The probability of any number (except
zero) is the same.
• Statistically it is smoother than a really
random function.
• This is the mechanism used in most
random number generation in calculators
and languages
The m-sequence
• The sequence is fixed – these are not
random numbers, they are pseudorandom
numbers.
• The generator is sometimes run until
interrupted by an external event (e.g. me
pressing a key), making the next number
unpredictable.
• If we always seed using the same number,
the sequence will always be the same (e.g.
for debugging)
• Many languages seed using the system clock
Random numbers in high level
languages
• “C” has:
• rand() – Produces a random integer
• srand() – Sets up a seed for rand()
// % 6 means modulo 6,
// ie the remainder when the
// number is divided by 6.
// This is not the best way
Random numbers in high level
languages
• Dice – a better algorithm:
seed = time(NULL);
srand(seed);
x := ((rand()/(RAND_MAX+1));
y := (x * 6) + 1;
i = int(y);
Coins
Probability
3 2+1 1+2 3
M-Sequence
Number/Enumeration
Decisions using random numbers
• The Boxer:
• The boxer can use a Jab (j), an Uppercut
(u), a Cross (c) or a Hook (h)
• In this first example, we want all to be
equally likely:
The Boxer
x := int(4*Random());
SELECT CASE (x)
CASE (0)
move := “j”
CASE (1)
move := “u”
CASE (2)
move := “c”
CASE default
move := “h”
END SELECT
The Boxer
• In real life a boxer would use a jab far
more often than any other move
• So we assign our probability space
accordingly. for r being a random number
in the range 0-1.0:
– 0... 0.4 means Jab
– 0.4... 0.6 means uppercut
– 0.6... 0.8 means cross
– 0.8... 1.0 means hook
The Boxer
• What have we just done?
• We’ve used a random number generator
to “decide” on an AI opponent’s next
move.
• We’ve adjusted the probabilities so that
the choice of move is more realistic.
Star Field Generation
• In Elite, there was a 2-D star-field
• The field was based on an N x N grid
• Whether there was a star in a cell or not
was decided by a seeded random number
generator
Star := 0;
IF (random() < 0.1 THEN Star := 1;
Star Field Generation
• Why was the random number seeded?
• Well, the BBC Microcomputer only had
32k of memory.
• Since the generator was seeded, it
produced the same result every time it ran
• So we didn’t need to store the star map in
memory – we just ran the procedure.
• A similar seeded procedure decided on
the planets around the stars
Terrain
• We need to generate terrain for a
landscape in which our hero is to fight
• We don’t especially want to specify and
remember the whole world – it’s easier if
we can generate it on the fly, like our star
map
• There are a number of ways of doing this
Terrain
• A crude way:
Overlapping hills
Background Sound
• Background game music (extra-diegetic
music) is usually looped because we don’t
know how long the scene will last.
• However, soundscapes cannot be looped
for long. After a while you can predict the
arrival of the cockerel’s crow (again!)
• (Your best bet with a looped soundscape
is to make it complicated and long)
Background Sound
• A better solution is the GRANULAR
SOUND SYNTHESIS technique, where
sound effects are added at random with a
certain probability.
• Sound effects may be randomly altered
also: pitch and playback speed may be
altered to hide the fact that they have the
same source.
Chip Music Percussion
• When there is a game sound chip
(Gameboy), percussion is often generated
using filtered white noise.
• Digital white noise is a random sequence
of numbers.
• This is usually generated on-chip using an
M-sequence generator programmed into
the hardware.