bronx.compat.random¶
The Random class from this module will produce results that are
identical to the Python2 version.
If you do not care about random number generation reproducibility, please use
the bare random.Random class.
Classes¶
- class bronx.compat.random.Random(x=None)[source]¶
Bases:
RandomThe Python3 version of the Python’s 2.7 Random class.
This is mostly a cut&paste of Python2.7’ code. Sorry for that.
Initialize an instance.
Optional argument x controls seeding, as for Random.seed().
- randrange(start, stop=None, step=1, _int=<class 'int'>, _maxwidth=9007199254740992)[source]¶
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.
- Note:
This is a cut&paste of the old Python2 code !
- sample(population, k)[source]¶
Chooses k unique random elements from a population sequence.
Returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples. This allows raffle winners (the sample) to be partitioned into grand prize and second place winners (the subslices).
Members of the population need not be hashable or unique. If the population contains repeats, then each occurrence is a possible selection in the sample.
To choose a sample in a range of integers, use xrange as an argument. This is especially fast and space efficient for sampling from a large population: sample(xrange(10000000), 60)