annotate Lib/whrandom.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 4c01ee0a8073
children e23381fa4750
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
1 """Wichman-Hill random number generator.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
2
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
3 Wichmann, B. A. & Hill, I. D. (1982)
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
4 Algorithm AS 183:
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
5 An efficient and portable pseudo-random number generator
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
6 Applied Statistics 31 (1982) 188-190
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
7
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
8 see also:
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
9 Correction to Algorithm AS 183
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
10 Applied Statistics 33 (1984) 123
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
11
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
12 McLeod, A. I. (1985)
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
13 A remark on Algorithm AS 183
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
14 Applied Statistics 34 (1985),198-200
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
17 USE:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
18 whrandom.random() yields double precision random numbers
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
19 uniformly distributed between 0 and 1.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
20
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
21 whrandom.seed(x, y, z) must be called before whrandom.random()
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
22 to seed the generator
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
23
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
24 There is also an interface to create multiple independent
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
25 random generators, and to choose from other ranges.
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
8456
92c88ca62da7 Add comments explaining thread unsafety of this code.
Guido van Rossum <guido@python.org>
parents: 7312
diff changeset
28
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
29 Multi-threading note: the random number generator used here is not
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
30 thread-safe; it is possible that nearly simultaneous calls in
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
31 different theads return the same random value. To avoid this, you
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
32 have to use a lock around all calls. (I didn't want to slow this
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
33 down in the serial case by using a lock here.)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
34 """
8456
92c88ca62da7 Add comments explaining thread unsafety of this code.
Guido van Rossum <guido@python.org>
parents: 7312
diff changeset
35
11870
d7eca5b0960a Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
36 # Translated by Guido van Rossum from C source provided by
d7eca5b0960a Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
37 # Adrian Baddeley.
d7eca5b0960a Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
38
d7eca5b0960a Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents: 11707
diff changeset
39
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
40 class whrandom:
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
41 def __init__(self, x = 0, y = 0, z = 0):
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
42 """Initialize an instance.
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
43 Without arguments, initialize from current time.
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
44 With arguments (x, y, z), initialize from them."""
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
45 self.seed(x, y, z)
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
46
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
47 def seed(self, x = 0, y = 0, z = 0):
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
48 """Set the seed from (x, y, z).
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
49 These must be integers in the range [0, 256)."""
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
50 if not type(x) == type(y) == type(z) == type(0):
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
51 raise TypeError, 'seeds must be integers'
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
52 if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
53 raise ValueError, 'seeds must be in range(0, 256)'
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
54 if 0 == x == y == z:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
55 # Initialize from current time
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
56 import time
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
57 t = long(time.time() * 256)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
58 t = int((t&0xffffff) ^ (t>>24))
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
59 t, x = divmod(t, 256)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
60 t, y = divmod(t, 256)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
61 t, z = divmod(t, 256)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
62 # Zero is a poor seed, so substitute 1
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
63 self._seed = (x or 1, y or 1, z or 1)
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
64
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
65 def random(self):
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
66 """Get the next random number in the range [0.0, 1.0)."""
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
67 # This part is thread-unsafe:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
68 # BEGIN CRITICAL SECTION
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
69 x, y, z = self._seed
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
70 #
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
71 x = (171 * x) % 30269
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
72 y = (172 * y) % 30307
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
73 z = (170 * z) % 30323
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
74 #
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
75 self._seed = x, y, z
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
76 # END CRITICAL SECTION
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
77 #
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
78 return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
79
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
80 def uniform(self, a, b):
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
81 """Get a random number in the range [a, b)."""
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
82 return a + (b-a) * self.random()
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
83
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
84 def randint(self, a, b):
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
85 """Get a random integer in the range [a, b] including
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
86 both end points.
13239
650154292ee1 Line-wrap and properly indent a couple of docstrings.
Fred Drake <fdrake@acm.org>
parents: 11870
diff changeset
87
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
88 (Deprecated; use randrange below.)"""
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
89 return self.randrange(a, b+1)
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
90
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
91 def choice(self, seq):
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
92 """Choose a random element from a non-empty sequence."""
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
93 return seq[int(self.random() * len(seq))]
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 8920
diff changeset
94
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
95 def randrange(self, start, stop=None, step=1, int=int, default=None):
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
96 """Choose a random item from range(start, stop[, step]).
13239
650154292ee1 Line-wrap and properly indent a couple of docstrings.
Fred Drake <fdrake@acm.org>
parents: 11870
diff changeset
97
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
98 This fixes the problem with randint() which includes the
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
99 endpoint; in Python this is usually not what you want.
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
100 Do not supply the 'int' and 'default' arguments."""
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
101 # This code is a bit messy to make it fast for the
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
102 # common case while still doing adequate error checking
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
103 istart = int(start)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
104 if istart != start:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
105 raise ValueError, "non-integer arg 1 for randrange()"
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
106 if stop is default:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
107 if istart > 0:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
108 return int(self.random() * istart)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
109 raise ValueError, "empty range for randrange()"
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
110 istop = int(stop)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
111 if istop != stop:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
112 raise ValueError, "non-integer stop for randrange()"
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
113 if step == 1:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
114 if istart < istop:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
115 return istart + int(self.random() *
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
116 (istop - istart))
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
117 raise ValueError, "empty range for randrange()"
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
118 istep = int(step)
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
119 if istep != step:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
120 raise ValueError, "non-integer step for randrange()"
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
121 if istep > 0:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
122 n = (istop - istart + istep - 1) / istep
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
123 elif istep < 0:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
124 n = (istop - istart + istep + 1) / istep
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
125 else:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
126 raise ValueError, "zero step for randrange()"
8801
61464037da53 Introducing randrange([start,] stop [,step]) -- same as
Guido van Rossum <guido@python.org>
parents: 8456
diff changeset
127
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
128 if n <= 0:
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
129 raise ValueError, "empty range for randrange()"
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 13239
diff changeset
130 return istart + istep*int(self.random() * n)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 # Initialize from the current time
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1178
diff changeset
134 _inst = whrandom()
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
135 seed = _inst.seed
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
136 random = _inst.random
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
137 uniform = _inst.uniform
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
138 randint = _inst.randint
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
139 choice = _inst.choice
8801
61464037da53 Introducing randrange([start,] stop [,step]) -- same as
Guido van Rossum <guido@python.org>
parents: 8456
diff changeset
140 randrange = _inst.randrange