75 Commits

Author SHA1 Message Date
Martin v. Löwis
0cc3ea620c Revert the following revisions, as they don't fix security
problems:
52448, 52468, 52472, 52475, 52646, 52797, 52802, 52863, 52999, 53001,
53101, 53371, 53373, 53383, 53384, 53736, 53812, 53921, 55578, 55580,
55581, 55772, 55775, 56557, 57093, 57094, 58630, 60114
2008-03-02 17:47:51 +00:00
Raymond Hettinger
7c6e470067 Bug #1590891: random.randrange don't return correct value for big number 2006-12-20 08:23:39 +00:00
Tim Peters
b94051562c Merge rev 46840 from trunk.
shuffle() doscstring:  Removed warning about sequence length
versus generator period.  While this was a real weakness of the
older WH generator for lists with just a few dozen elements,
and so could potentially bite the naive ;-), the Twister should
show excellent behavior up to at least 600 elements.
2006-06-10 22:54:19 +00:00
Tim Peters
1c63b55984 Merge rev 43511 from the trunk.
Another crack at bug #1460340:  make random.sample(dict)
work, this time by ugly brute force.
2006-04-01 00:41:10 +00:00
Raymond Hettinger
e76415303f SF bug #1460340: random.sample can raise KeyError
Fix the hit and miss style of testing for sets and dicts.
2006-03-29 08:54:54 +00:00
Raymond Hettinger
75634db73f Sync-up with head except for 1.72 which is Py2.5 specific. 2005-09-15 18:07:11 +00:00
Raymond Hettinger
ffdb8bb99c Use floor division operator. 2004-09-27 15:29:05 +00:00
Raymond Hettinger
23f1241dc6 SF #1027105: HardwareRandom should be renamed OSRandom
Renamed the new generator at Trevor's recommendation.
The name HardwareRandom suggested a bit more than it
delivered (no radioactive decay detectors or such).
2004-09-13 22:23:21 +00:00
Raymond Hettinger
c1c43cad63 Fulfill Martin's request to use try/except rather than a "look before
you leap" approach.  Makes the early call to os.urandom() unnecessary.
2004-09-05 00:00:42 +00:00
Raymond Hettinger
bdbdfd731d SF bug #1022010: Import random fails
* trap NotImplementedError raised by os.urandom calls when not available
  on a particular system.
2004-09-03 19:09:22 +00:00
Tim Peters
7c2a85b2d4 HardwareRandom: Go back to multiplying by 2**-BPF instead of using
ldexp.  Both methods are exact, and return the same results.  Turns out
multiplication is a few (but just a few) percent faster on my box.
They're both significantly faster than using struct with a Q format
to convert bytes to a 64-bit long (struct.unpack() appears to lose due
to the tuple creation/teardown overhead), and calling _hexlify is
significantly faster than doing bytes.encode('hex').  So we appear to
have hit a local minimum (wrt speed) here.
2004-08-31 02:19:55 +00:00
Raymond Hettinger
3fa19d7ff8 Take advantage of the math library's ldexp for assembling a float by
components without division and without roundoff error for properly
sized mantissas (i.e. on systems with 53 or more mantissa bits per
float).  Eliminates the previous implementation's rounding bias as
aptly demonstrated by Tim Peters.
2004-08-31 01:05:15 +00:00
Raymond Hettinger
356a4599ac Teach the random module about os.urandom().
* Use it for seeding when it is available.
* Provide an alternate generator based on it.
2004-08-30 06:14:31 +00:00
Raymond Hettinger
5dae505bbd Fix typo in comment. 2004-06-07 02:07:15 +00:00
Raymond Hettinger
9021c95595 SF bug #964230: random.choice([]) should return more intelligible exception
Add a comment to make the traceback less mysterious.
2004-06-05 14:53:22 +00:00
Jeremy Hylton
2b55d35850 Reflow long line. 2004-02-23 17:27:57 +00:00
Tim Peters
58eb11cf62 Whitespace normalization. 2004-01-18 20:29:55 +00:00
Raymond Hettinger
85c20a41df Implement and apply PEP 322, reverse iteration 2003-11-06 14:06:48 +00:00
Raymond Hettinger
411c602349 Minor fixup. "Random" was listed twice in __all__. 2003-10-12 17:14:11 +00:00
Raymond Hettinger
2f726e9093 SF bug #812202: randint is always even
* Added C coded getrandbits(k) method that runs in linear time.
* Call the new method from randrange() for ranges >= 2**53.
* Adds a warning for generators not defining getrandbits() whenever they
  have a call to randrange() with too large of a population.
2003-10-05 09:09:15 +00:00
Raymond Hettinger
66d09f1b30 SF bug #801342: Bug (documentation or real, your choice) in random.sample.
random.sample() uses one of two algorithms depending on the ratio of the
sample size to the population size.  One of the algorithms accepted any
iterable population argument so long as it defined __len__().  The other
had a stronger requirement that the population argument be indexable.

While it met the documentation specifications which insisted that the
population argument be a sequence, it made random.sample() less usable
with sets.  So, the second algorithm was modified to coerce non-indexable
iterables and dictionaries into a tuple before proceeding.
2003-09-06 04:25:54 +00:00
Raymond Hettinger
6229713221 Eliminate unnecessary eval() in tests 2003-08-30 01:24:19 +00:00
Raymond Hettinger
3081d59f92 SF bug #778964: bad seed in python 2.3 random
The default seed is time.time().
Multiplied by 256 before truncating so that fractional seconds are used.
This way, two successive calls to random.seed() are much more likely
to produce different sequences.
2003-08-09 18:30:57 +00:00
Raymond Hettinger
f8a52d38ad Removed deprecated functions 2003-08-05 12:23:19 +00:00
Raymond Hettinger
5f078ff7f0 SF bug #759889: Pickling of Random is broken
* Implement __reduce__() to support pickling.
* Add a test case to prove a successful roundtrip through pickle.
2003-06-24 20:29:04 +00:00