Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
whrandom Class Reference

Public Member Functions

def __init__
 
def seed
 
def random
 
def uniform
 
def randint
 
def choice
 
def randrange
 

Detailed Description

Definition at line 40 of file whrandom.py.

Constructor & Destructor Documentation

def __init__ (   self,
  x = 0,
  y = 0,
  z = 0 
)
Initialize an instance.
Without arguments, initialize from current time.
With arguments (x, y, z), initialize from them.

Definition at line 41 of file whrandom.py.

References whrandom.seed().

41 
42  def __init__(self, x = 0, y = 0, z = 0):
43  """Initialize an instance.
44  Without arguments, initialize from current time.
45  With arguments (x, y, z), initialize from them."""
46  self.seed(x, y, z)

Member Function Documentation

def choice (   self,
  seq 
)
Choose a random element from a non-empty sequence.

Definition at line 91 of file whrandom.py.

References whrandom.random().

91 
92  def choice(self, seq):
93  """Choose a random element from a non-empty sequence."""
94  return seq[int(self.random() * len(seq))]
def randint (   self,
  a,
  b 
)
Get a random integer in the range [a, b] including
both end points.

(Deprecated; use randrange below.)

Definition at line 84 of file whrandom.py.

References whrandom.randrange().

84 
85  def randint(self, a, b):
86  """Get a random integer in the range [a, b] including
87  both end points.
88 
89  (Deprecated; use randrange below.)"""
90  return self.randrange(a, b+1)
def random (   self)
Get the next random number in the range [0.0, 1.0).

Definition at line 65 of file whrandom.py.

References whrandom._seed.

65 
66  def random(self):
67  """Get the next random number in the range [0.0, 1.0)."""
68  # This part is thread-unsafe:
69  # BEGIN CRITICAL SECTION
70  x, y, z = self._seed
71  #
72  x = (171 * x) % 30269
73  y = (172 * y) % 30307
74  z = (170 * z) % 30323
75  #
76  self._seed = x, y, z
77  # END CRITICAL SECTION
78  #
79  return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
def randrange (   self,
  start,
  stop = None,
  step = 1,
  int = int,
  default = None 
)
Choose a random item from range(start, stop[, step]).

This fixes the problem with randint() which includes the
endpoint; in Python this is usually not what you want.
Do not supply the 'int' and 'default' arguments.

Definition at line 95 of file whrandom.py.

References whrandom.random().

95 
96  def randrange(self, start, stop=None, step=1, int=int, default=None):
97  """Choose a random item from range(start, stop[, step]).
98 
99  This fixes the problem with randint() which includes the
100  endpoint; in Python this is usually not what you want.
101  Do not supply the 'int' and 'default' arguments."""
102  # This code is a bit messy to make it fast for the
103  # common case while still doing adequate error checking
104  istart = int(start)
105  if istart != start:
106  raise ValueError, "non-integer arg 1 for randrange()"
107  if stop is default:
108  if istart > 0:
109  return int(self.random() * istart)
110  raise ValueError, "empty range for randrange()"
111  istop = int(stop)
112  if istop != stop:
113  raise ValueError, "non-integer stop for randrange()"
114  if step == 1:
115  if istart < istop:
116  return istart + int(self.random() *
117  (istop - istart))
118  raise ValueError, "empty range for randrange()"
119  istep = int(step)
120  if istep != step:
121  raise ValueError, "non-integer step for randrange()"
122  if istep > 0:
123  n = (istop - istart + istep - 1) / istep
124  elif istep < 0:
125  n = (istop - istart + istep + 1) / istep
126  else:
127  raise ValueError, "zero step for randrange()"
128 
129  if n <= 0:
130  raise ValueError, "empty range for randrange()"
131  return istart + istep*int(self.random() * n)
132 
133 
# Initialize from the current time
def seed (   self,
  x = 0,
  y = 0,
  z = 0 
)
Set the seed from (x, y, z).
These must be integers in the range [0, 256).

Definition at line 47 of file whrandom.py.

References whrandom._seed.

47 
48  def seed(self, x = 0, y = 0, z = 0):
49  """Set the seed from (x, y, z).
50  These must be integers in the range [0, 256)."""
51  if not type(x) == type(y) == type(z) == type(0):
52  raise TypeError, 'seeds must be integers'
53  if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
54  raise ValueError, 'seeds must be in range(0, 256)'
55  if 0 == x == y == z:
56  # Initialize from current time
57  import time
58  t = long(time.time() * 256)
59  t = int((t&0xffffff) ^ (t>>24))
60  t, x = divmod(t, 256)
61  t, y = divmod(t, 256)
62  t, z = divmod(t, 256)
63  # Zero is a poor seed, so substitute 1
64  self._seed = (x or 1, y or 1, z or 1)
def uniform (   self,
  a,
  b 
)
Get a random number in the range [a, b).

Definition at line 80 of file whrandom.py.

References whrandom.random().

80 
81  def uniform(self, a, b):
82  """Get a random number in the range [a, b)."""
83  return a + (b-a) * self.random()

The documentation for this class was generated from the following file: