forked from ninas/umonya_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.html
81 lines (60 loc) · 3.05 KB
/
random.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Introductory Programming in Python: Random Numbers</title>
<link rel='stylesheet' type='text/css' href='style.css' />
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<script src="animation.js" type="text/javascript">
</script>
</head>
<body onload="animate_loop()">
<div class="page">
<h1>Introductory Programming in Python: Lesson 17<br />
Random Numbers</h1>
<div class="centered">
[<a href="commandline_arguments.html">Prev: More on Command Line Arguments</a>] [<a href="index.html">Course Outline</a>] [<a href="files.html">Next: Files for Input and Output</a>]
</div>
<h2>Exploring the random Module Further</h2>
<p>Although nearly everything the <a class="doclink"
href='http://docs.python.org/lib/module-random.html'>random module</a>
does is based on obtaining a single random number, the module provides
us with a number of convenience functions, that take the tedium from
doing common 'random' tasks. Functions are provided which return random
integers in given ranges, random choices from sequences, or produce
random permutations of a sequence. In addition there are some fairly
useful distribution functions, which pull random number from chosen
distributions with specified parameters.</p>
<p>Let's deal with the integers first.</p>
<ul>
<li><code>random.randrange([start,] <stop>[, step])</code>
returns a single random integer from <em>start</em> to
<em>stop</em>-1. If <em>start</em> is not specified, it defaults to
0. If <em>step</em> is specified, only integers in increments of
<em>step</em> from <em>start</em> will be chosen.</li>
<li><code>random.randint(<start>, <stop>)</code>
returns a single random integer from <em>start</em> to
<em>stop</em> (inclusive).</li>
</ul>
<p>The true convenience of the random module becomes apparent with the
three sequence functions.</p>
<ul>
<li><code>random.choice(<sequence>)</code> returns a single
random element from the sequence</li>
<li><code>random.shuffle(<sequence>)</code> shuffles the
elements in sequence, in place, so they are in random order.</li>
<li><code>random.sample(<sequence>, <k>)</code> returns
<em>k</em> randomly chosen elements from the sequence. This is
sampling without replacement, i.e. a single element from the
sequence can be chosen at most once.</li>
</ul>
<h2>Exercises</h2>
<div class="centered">
[<a href="commandline_arguments.html">Prev: More on Command Line Arguments</a>] [<a href="index.html">Course Outline</a>] [<a href="parsing.html">Next: Basic Parsing</a>]
</div>
</div>
<div class="pagefooter">
Copyright © James Dominy 2007-2008; Released under the <a href="http://www.gnu.org/copyleft/fdl.html">GNU Free Documentation License</a><br />
<a href="intropython.tar.gz">Download the tarball</a>
</div>
</body>
</html>