File: random.rst

package info (click to toggle)
firmware-microbit-micropython 1.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 25,448 kB
  • sloc: ansic: 83,496; cpp: 27,664; python: 2,475; asm: 274; makefile: 245; javascript: 41; sh: 25
file content (68 lines) | stat: -rw-r--r-- 1,668 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Random Number Generation
************************

.. py:module:: random

This module is based upon the ``random`` module in the Python standard library.
It contains functions for generating random behaviour.

To access this module you need to::

    import random

We assume you have done this for the examples below.

Functions
=========

.. py:function:: getrandbits(n)

    Returns an integer with ``n`` random bits.

.. warning::

    Because the underlying generator function returns at most 30 bits, ``n``
    may only be a value between 1-30 (inclusive).

.. py:function:: seed(n)

    Initialize the random number generator with a known integer ``n``. This
    will give you reproducibly deterministic randomness from a given starting
    state (``n``).


.. py:function:: randint(a, b)

    Return a random integer ``N`` such that ``a <= N <= b``. Alias for
    ``randrange(a, b+1)``.


.. py:function:: randrange(stop)

    Return a randomly selected integer between zero and up to (but not
    including) ``stop``.

.. py:function:: randrange(start, stop)

    Return a randomly selected integer from ``range(start, stop)``.

.. py:function:: randrange(start, stop, step)

    Return a randomly selected element from ``range(start, stop, step)``.


.. py:function:: choice(seq)

    Return a random element from the non-empty sequence ``seq``. If ``seq`` is
    empty, raises ``IndexError``.


.. py:function:: random()

    Return the next random floating point number in the range [0.0, 1.0)


.. py:function:: uniform(a, b)

    Return a random floating point number ``N`` such that ``a <= N <= b``
    for ``a <= b`` and ``b <= N <= a`` for ``b < a``.