Seite - 55 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 55 -
Text der Seite - 55 -
2.4 RandomNumbers 55
In [3]: random.randint(1, 6)
Out[3]: 5
The random module contains also other useful functions, two of which are
random (yes, same name as the module) and uniform. Both of these functions
return a floating point number from an interval where each number has equal
probability of being drawn. For random, the interval is always [0,1) (i.e. 0 is
included, but 1 is not), while uniform requires the programmer to specify the
interval [a,b] (wherebotha andbare included14).Thefunctionsareusedsimilarly
torandint, so, interactively,wemayforexampledo:
In [1]: import random
In [2]: x = random.random() # draw float from [0, 1), assign to x
In [3]: y = random.uniform(10, 20) # ...float from [10, 20], assign to y
In [4]: print(’x = {:g}, y = {:g}’.format(x, y))
Out[5]: x = 0.714621 , y = 13.1233
Drawing Many Random Numbers at a Time You have now met three useful
functions from the randommodule in Python’s standard library and seen them in
simple use. However, each of those functions provides only a single number with
each functioncall. If you need manypseudo-randomnumbers,one option is to use
such function calls inside a loop (Chap.3). Another (faster) alternative, is to rather
usefunctionsthatallowvectorizeddrawingof thenumbers,so thatasinglefunction
call provides all the numbers you need in one go. Such functionality is offered by
another module, which also happens to be calledrandom, but which resides in the
numpy library. All three functions demonstrated above have their counterparts in
numpyand we might show interactivelyhoweach of these can be used to generate,
e.g., fournumberswithone functioncall.
In [1]: import numpy as np
In [2]: np.random.randint(1, 6, 4) # ...4 integers from [1, 6)
Out[2]: array([1, 3, 5, 3])
In [3]: np.random.random(4) # ...4 floats from [0, 1)
Out[3]: array([ 0.79183276, 0.01398365, 0.04982849, 0.11630963])
In [4]: np.random.uniform(10, 20, 4) # ...4 floats from [10, 20)
Out[4]: array([ 10.95846078, 17.3971301 , 19.73964488, 18.14332234])
In each case, the size argument is here set to 4 and an array is returned.Of course,
with the size argument, you may ask for thousands of numbers if you like. As is
evident fromthe interval specifications in the code,noneof these functions include
the upper interval limit. However, if we wanted, e.g., randint to have 6 as the
inclusiveupper limit, wecouldsimplygive7as thesecondargument in stead.
14 Strictlyspeaking,bmayormaynotbeincluded(http://docs.python.org/),dependingonfloating-
point rounding in the equationa + (b-a)*random().
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik