16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

228 CHAPTER 10 ■ BATTERIES INCLUDED<br />

random<br />

The random module contains functions that return random numbers, which can be useful for<br />

simulations or any program that generates random output.<br />

■Note Actually, the numbers generated are pseudo-random. That means that while they appear completely<br />

random, there is a predictable system that underlies them. However, because the module is so good at<br />

pretending <strong>to</strong> be random, you probably won’t ever have <strong>to</strong> worry about this (unless you want <strong>to</strong> use these<br />

numbers for strong-cryp<strong>to</strong>graphy purposes, in which case they may not be “strong” enough <strong>to</strong> withstand<br />

determined attack—but if you’re in<strong>to</strong> strong cryp<strong>to</strong>graphy, you surely don’t need me <strong>to</strong> explain such elementary<br />

issues). If you need real randomness, you should check out the urandom function of the os module. The class<br />

SystemRandom in the random module is based on the same kind of functionality, and gives you data that is<br />

close <strong>to</strong> real randomness.<br />

Some important functions in this module are shown in Table 10-7.<br />

Table 10-7. Some Important Functions in the random Module<br />

Function<br />

Description<br />

random() Returns a random real number n such that 0 ≤ n < 1<br />

getrandbits(n)<br />

uniform(a, b)<br />

randrange([start], s<strong>to</strong>p, [step])<br />

choice(seq)<br />

shuffle(seq[, random])<br />

sample(seq, n)<br />

Returns n random bits, in the form of a long integer<br />

Returns a random real number n such that a ≤ n < b<br />

Returns a random number from range(start, s<strong>to</strong>p,<br />

step)<br />

Returns a random element from the sequence seq<br />

Shuffles the sequence seq in place<br />

Chooses n random, unique elements from the<br />

sequence seq<br />

The function random.random is one of the most basic random functions; it simply returns<br />

a pseudo-random number n such that 0 < n < 1. Unless this is exactly what you need, you<br />

should probably use one of the other functions, which offer extra functionality. The function<br />

random.getrandbits returns a given number of bits (binary digits), in the form of a long integer.<br />

This is probably mostly useful if you’re really in<strong>to</strong> random stuff (for example, working with<br />

cryp<strong>to</strong>graphy).<br />

The function random.uniform, when supplied with two numerical parameters a and b,<br />

returns a random (uniformly distributed) real number n such that a < n < b. So, for example,<br />

if you want a random angle, you could use uniform(0,360).<br />

The function random.randrange is the standard function for generating a random integer<br />

in the range you would get by calling range with the same arguments. For example, <strong>to</strong> get a

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!