You are on page 1of 1

IAS Practice Paragraph IA.

S - Other random number generators

In th is case, drunk generates random integers between O and 127, moving


between succeeding values by adding a number between -4 and 4. Try generat-
ing a series of numbers and you w ill see that between one number and the next
there is a jump that is always less than 5 - the object implements what is called
a "random wa lk" (or a "drun kard's walk"). Add a numbe r box to its center inlet
(w hich corresponds to t he maximum number that can be generated, minus 1)
and also to the right inlet (wh ich corresponds to the maximum leap that can
occur between numbers, again minus 1). and carefully observe the numbers that
are generated, as well as how th e random walk changes step by step.

Let's recap some of the random objects t hat we 've seen:

random Generates random numbers every t ime that it receives a bang.


It can take a numeric argument that indicates the interval over
wh ich to generate numbers: if w e represent the argument as
n, numbers w ill fall with in t he range of O to n-1.
vs . random Generates random float ing point numbers betwe en O and 1
vs.between Generates either integer or floating point random nu mbers
between a minimum and a maximum.
drunk Accepts two arguments and generates random num bers
between O and the fi rst argument minus 1, exactly as random
does. Two values generated in succession, however, can vary
by no more than a distance random ly chosen from the interval
betw een O and t he second argum ent minus 1. In other words,
the object implements a random wa lk between numbers.

The drunk object, like random, generates positive integer numbers wi thin an
interval that begins at 0. It is possible to t ransform th is int erval into an arbi-
trary interval by using the technique that we examined in Section IA.2 (On t he
Generation of Random Numbers); see in particu lar f igure IA. 16 and the version
of the algori th m within th e subpatch in f igure IA.28 . In f igure IA.38, we see an
adaptation of th is algorithm fo r drunk :

tJ iJ minimum t1J
,---------j
r
maximum

...
J maximum interval between
~
drunk a number and ",ts successor

f7"7- ►•:;;;-- ~ inim: m value to be used as an


~ argument to the addition operator
+
0
'11) generated number between the
minimum and the maximum

Fig. IA.38 drunk w ith minimum and maximum values


154

You might also like