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

Skip to content

Commit f2bd04f

Browse files
authored
Improve recipe readability (GH-22685)
1 parent 7992579 commit f2bd04f

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

Doc/library/random.rst

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ Functions for sequences
253253
order so that the sample is reproducible.
254254

255255

256+
.. _real-valued-distributions:
257+
256258
Real-valued distributions
257259
-------------------------
258260

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

521+
.. seealso::
522+
523+
`Statistics for Hackers <https://www.youtube.com/watch?v=Iq9DzN6mvYA>`_
524+
a video tutorial by
525+
`Jake Vanderplas <https://us.pycon.org/2016/speaker/profile/295/>`_
526+
on statistical analysis using just a few fundamental concepts
527+
including simulation, sampling, shuffling, and cross-validation.
528+
529+
`Economics Simulation
530+
<http://nbviewer.jupyter.org/url/norvig.com/ipython/Economics.ipynb>`_
531+
a simulation of a marketplace by
532+
`Peter Norvig <http://norvig.com/bio.html>`_ that shows effective
533+
use of many of the tools and distributions provided by this module
534+
(gauss, uniform, sample, betavariate, choice, triangular, and randrange).
535+
536+
`A Concrete Introduction to Probability (using Python)
537+
<http://nbviewer.jupyter.org/url/norvig.com/ipython/Probability.ipynb>`_
538+
a tutorial by `Peter Norvig <http://norvig.com/bio.html>`_ covering
539+
the basics of probability theory, how to write simulations, and
540+
how to perform data analysis using Python.
541+
542+
519543
Recipes
520544
-------
521545

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

528552
The following recipe takes a different approach. All floats in the
529-
interval are possible selections. Conceptually it works by choosing
530-
from evenly spaced multiples of 2⁻¹⁰⁷⁴ and then rounding down to the
531-
nearest representable float.
532-
533-
For efficiency, the actual mechanics involve calling
534-
:func:`~math.ldexp` to construct a representable float. The mantissa
535-
comes from a uniform distribution of integers in the range *2⁵² ≤
536-
mantissa < 2⁵³*. The exponent comes from a geometric distribution
537-
where exponents smaller than *-53* occur half as often as the next
538-
larger exponent.
553+
interval are possible selections. The mantissa comes from a uniform
554+
distribution of integers in the range *2⁵² ≤ mantissa < 2⁵³*. The
555+
exponent comes from a geometric distribution where exponents smaller
556+
than *-53* occur half as often as the next larger exponent.
539557

540558
::
541559

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

556-
All of the real valued distributions will use the new method::
574+
All :ref:`real valued distributions <real-valued-distributions>`
575+
in the class will use the new method::
557576

558577
>>> fr = FullRandom()
559578
>>> fr.random()
560579
0.05954861408025609
561580
>>> fr.expovariate(0.25)
562581
8.87925541791544
563582

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

565-
.. seealso::
566-
567-
`Statistics for Hackers <https://www.youtube.com/watch?v=Iq9DzN6mvYA>`_
568-
a video tutorial by
569-
`Jake Vanderplas <https://us.pycon.org/2016/speaker/profile/295/>`_
570-
on statistical analysis using just a few fundamental concepts
571-
including simulation, sampling, shuffling, and cross-validation.
572-
573-
`Economics Simulation
574-
<http://nbviewer.jupyter.org/url/norvig.com/ipython/Economics.ipynb>`_
575-
a simulation of a marketplace by
576-
`Peter Norvig <http://norvig.com/bio.html>`_ that shows effective
577-
use of many of the tools and distributions provided by this module
578-
(gauss, uniform, sample, betavariate, choice, triangular, and randrange).
579589

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

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

0 commit comments

Comments
 (0)