Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve recipe readability #22685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 37 additions & 31 deletions Doc/library/random.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ Functions for sequences
order so that the sample is reproducible.


.. _real-valued-distributions:

Real-valued distributions
-------------------------

Expand Down Expand Up @@ -516,26 +518,42 @@ Simulation of arrival times and service deliveries for a multiserver queue::
print(f'Mean wait: {mean(waits):.1f} Max wait: {max(waits):.1f}')
print('Quartiles:', [round(q, 1) for q in quantiles(waits)])

.. seealso::

`Statistics for Hackers <https://www.youtube.com/watch?v=Iq9DzN6mvYA>`_
a video tutorial by
`Jake Vanderplas <https://us.pycon.org/2016/speaker/profile/295/>`_
on statistical analysis using just a few fundamental concepts
including simulation, sampling, shuffling, and cross-validation.

`Economics Simulation
<http://nbviewer.jupyter.org/url/norvig.com/ipython/Economics.ipynb>`_
a simulation of a marketplace by
`Peter Norvig <http://norvig.com/bio.html>`_ that shows effective
use of many of the tools and distributions provided by this module
(gauss, uniform, sample, betavariate, choice, triangular, and randrange).

`A Concrete Introduction to Probability (using Python)
<http://nbviewer.jupyter.org/url/norvig.com/ipython/Probability.ipynb>`_
a tutorial by `Peter Norvig <http://norvig.com/bio.html>`_ covering
the basics of probability theory, how to write simulations, and
how to perform data analysis using Python.


Recipes
-------

The default :func:`.random` returns multiples of 2⁻⁵³ in the range
*0.0 ≤ x < 1.0*. All such numbers are evenly spaced and exactly
*0.0 ≤ x < 1.0*. All such numbers are evenly spaced and are exactly
representable as Python floats. However, many floats in that interval
are not possible selections. For example, ``0.05954861408025609``
isn't an integer multiple of 2⁻⁵³.

The following recipe takes a different approach. All floats in the
interval are possible selections. Conceptually it works by choosing
from evenly spaced multiples of 2⁻¹⁰⁷⁴ and then rounding down to the
nearest representable float.

For efficiency, the actual mechanics involve calling
:func:`~math.ldexp` to construct a representable float. The mantissa
comes from a uniform distribution of integers in the range *2⁵² ≤
mantissa < 2⁵³*. The exponent comes from a geometric distribution
where exponents smaller than *-53* occur half as often as the next
larger exponent.
interval are possible selections. The mantissa comes from a uniform
distribution of integers in the range *2⁵² ≤ mantissa < 2⁵³*. The
exponent comes from a geometric distribution where exponents smaller
than *-53* occur half as often as the next larger exponent.

::

Expand All @@ -553,35 +571,23 @@ larger exponent.
exponent += x.bit_length() - 32
return ldexp(mantissa, exponent)

All of the real valued distributions will use the new method::
All :ref:`real valued distributions <real-valued-distributions>`
in the class will use the new method::

>>> fr = FullRandom()
>>> fr.random()
0.05954861408025609
>>> fr.expovariate(0.25)
8.87925541791544

The recipe is conceptually equivalent to an algorithm that chooses from
all the multiples of 2⁻¹⁰⁷⁴ in the range *0.0 ≤ x < 1.0*. All such
numbers are evenly spaced, but most have to be rounded down to the
nearest representable Python float. (The value 2⁻¹⁰⁷⁴ is the smallest
positive unnormalized float and is equal to ``math.ulp(0.0)``.)

.. seealso::

`Statistics for Hackers <https://www.youtube.com/watch?v=Iq9DzN6mvYA>`_
a video tutorial by
`Jake Vanderplas <https://us.pycon.org/2016/speaker/profile/295/>`_
on statistical analysis using just a few fundamental concepts
including simulation, sampling, shuffling, and cross-validation.

`Economics Simulation
<http://nbviewer.jupyter.org/url/norvig.com/ipython/Economics.ipynb>`_
a simulation of a marketplace by
`Peter Norvig <http://norvig.com/bio.html>`_ that shows effective
use of many of the tools and distributions provided by this module
(gauss, uniform, sample, betavariate, choice, triangular, and randrange).

`A Concrete Introduction to Probability (using Python)
<http://nbviewer.jupyter.org/url/norvig.com/ipython/Probability.ipynb>`_
a tutorial by `Peter Norvig <http://norvig.com/bio.html>`_ covering
the basics of probability theory, how to write simulations, and
how to perform data analysis using Python.
.. seealso::

`Generating Pseudo-random Floating-Point Values
<https://allendowney.com/research/rand/downey07randfloat.pdf>`_ a
Expand Down