@@ -34,10 +34,8 @@ instance of the :class:`random.Random` class. You can instantiate your own
34
34
instances of :class: `Random ` to get generators that don't share state.
35
35
36
36
Class :class: `Random ` can also be subclassed if you want to use a different
37
- basic generator of your own devising: in that case, override the :meth: `~Random.random `,
38
- :meth: `~Random.seed `, :meth: `~Random.getstate `, and :meth: `~Random.setstate ` methods.
39
- Optionally, a new generator can supply a :meth: `~Random.getrandbits ` method --- this
40
- allows :meth: `randrange ` to produce selections over an arbitrarily large range.
37
+ basic generator of your own devising: see the documentation on that class for
38
+ more details.
41
39
42
40
The :mod: `random ` module also provides the :class: `SystemRandom ` class which
43
41
uses the system function :func: `os.urandom ` to generate random numbers
@@ -88,7 +86,7 @@ Bookkeeping functions
88
86
89
87
.. versionchanged :: 3.11
90
88
The *seed * must be one of the following types:
91
- * NoneType * , :class: `int `, :class: `float `, :class: `str `,
89
+ `` None `` , :class: `int `, :class: `float `, :class: `str `,
92
90
:class: `bytes `, or :class: `bytearray `.
93
91
94
92
.. function :: getstate()
@@ -388,6 +386,37 @@ Alternative Generator
388
386
``None ``, :class: `int `, :class: `float `, :class: `str `,
389
387
:class: `bytes `, or :class: `bytearray `.
390
388
389
+ Subclasses of :class: `!Random ` should override the following methods if they
390
+ wish to make use of a different basic generator:
391
+
392
+ .. method :: Random.seed(a=None, version=2)
393
+
394
+ Override this method in subclasses to customise the :meth: `~random.seed `
395
+ behaviour of :class: `!Random ` instances.
396
+
397
+ .. method :: Random.getstate()
398
+
399
+ Override this method in subclasses to customise the :meth: `~random.getstate `
400
+ behaviour of :class: `!Random ` instances.
401
+
402
+ .. method :: Random.setstate(state)
403
+
404
+ Override this method in subclasses to customise the :meth: `~random.setstate `
405
+ behaviour of :class: `!Random ` instances.
406
+
407
+ .. method :: Random.random()
408
+
409
+ Override this method in subclasses to customise the :meth: `~random.random `
410
+ behaviour of :class: `!Random ` instances.
411
+
412
+ Optionally, a custom generator subclass can also supply the following method:
413
+
414
+ .. method :: Random.getrandbits(k)
415
+
416
+ Override this method in subclasses to customise the
417
+ :meth: `~random.getrandbits ` behaviour of :class: `!Random ` instances.
418
+
419
+
391
420
.. class :: SystemRandom([seed])
392
421
393
422
Class that uses the :func: `os.urandom ` function for generating random numbers
@@ -421,30 +450,30 @@ Examples
421
450
422
451
Basic examples::
423
452
424
- >>> random() # Random float: 0.0 <= x < 1.0
453
+ >>> random() # Random float: 0.0 <= x < 1.0
425
454
0.37444887175646646
426
455
427
- >>> uniform(2.5, 10.0) # Random float: 2.5 <= x <= 10.0
456
+ >>> uniform(2.5, 10.0) # Random float: 2.5 <= x <= 10.0
428
457
3.1800146073117523
429
458
430
- >>> expovariate(1 / 5) # Interval between arrivals averaging 5 seconds
459
+ >>> expovariate(1 / 5) # Interval between arrivals averaging 5 seconds
431
460
5.148957571865031
432
461
433
- >>> randrange(10) # Integer from 0 to 9 inclusive
462
+ >>> randrange(10) # Integer from 0 to 9 inclusive
434
463
7
435
464
436
- >>> randrange(0, 101, 2) # Even integer from 0 to 100 inclusive
465
+ >>> randrange(0, 101, 2) # Even integer from 0 to 100 inclusive
437
466
26
438
467
439
- >>> choice(['win', 'lose', 'draw']) # Single random element from a sequence
468
+ >>> choice(['win', 'lose', 'draw']) # Single random element from a sequence
440
469
'draw'
441
470
442
471
>>> deck = 'ace two three four'.split()
443
- >>> shuffle(deck) # Shuffle a list
472
+ >>> shuffle(deck) # Shuffle a list
444
473
>>> deck
445
474
['four', 'two', 'ace', 'three']
446
475
447
- >>> sample([10, 20, 30, 40, 50], k=4) # Four samples without replacement
476
+ >>> sample([10, 20, 30, 40, 50], k=4) # Four samples without replacement
448
477
[40, 10, 50, 30]
449
478
450
479
Simulations::
@@ -551,14 +580,14 @@ Simulation of arrival times and service deliveries for a multiserver queue::
551
580
including simulation, sampling, shuffling, and cross-validation.
552
581
553
582
`Economics Simulation
554
- <https://nbviewer.jupyter. org/url/norvig.com/ipython/Economics.ipynb> `_
583
+ <https://nbviewer.org/url/norvig.com/ipython/Economics.ipynb> `_
555
584
a simulation of a marketplace by
556
585
`Peter Norvig <https://norvig.com/bio.html >`_ that shows effective
557
586
use of many of the tools and distributions provided by this module
558
587
(gauss, uniform, sample, betavariate, choice, triangular, and randrange).
559
588
560
589
`A Concrete Introduction to Probability (using Python)
561
- <https://nbviewer.jupyter. org/url/norvig.com/ipython/Probability.ipynb> `_
590
+ <https://nbviewer.org/url/norvig.com/ipython/Probability.ipynb> `_
562
591
a tutorial by `Peter Norvig <https://norvig.com/bio.html >`_ covering
563
592
the basics of probability theory, how to write simulations, and
564
593
how to perform data analysis using Python.
0 commit comments