Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

Commit b29e307

Browse files
authored
Merge pull request numba#7068 from jeertmans/random-seeding-warning
Enhancing docs about PRNG seeding
2 parents 25e406f + 4d6e802 commit b29e307

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎docs/source/reference/numpysupported.rst‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,42 @@ Initialization
582582

583583
* :func:`numpy.random.seed`: with an integer argument only
584584

585+
.. warning::
586+
Calling :func:`numpy.random.seed` from interpreted code (including from :term:`object mode`
587+
code) will seed the NumPy random generator, not the Numba random generator.
588+
To seed the Numba random generator, see the example below.
589+
590+
.. code-block:: python
591+
592+
from numba import njit
593+
import numpy as np
594+
595+
@njit
596+
def seed(a):
597+
np.random.seed(a)
598+
599+
@njit
600+
def rand():
601+
return np.random.rand()
602+
603+
604+
# Incorrect seeding
605+
np.random.seed(1234)
606+
print(rand())
607+
608+
np.random.seed(1234)
609+
print(rand())
610+
611+
# Correct seeding
612+
seed(1234)
613+
print(rand())
614+
615+
seed(1234)
616+
print(rand())
617+
618+
619+
620+
585621
Simple random data
586622
''''''''''''''''''
587623

‎docs/source/reference/pysupported.rst‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,9 +1156,39 @@ startup with entropy drawn from the operating system.
11561156
* :func:`random.vonmisesvariate`
11571157
* :func:`random.weibullvariate`
11581158

1159-
.. note::
1159+
.. warning::
11601160
Calling :func:`random.seed` from non-Numba code (or from :term:`object mode`
11611161
code) will seed the Python random generator, not the Numba random generator.
1162+
To seed the Numba random generator, see the example below.
1163+
1164+
.. code-block:: python
1165+
1166+
from numba import njit
1167+
import random
1168+
1169+
@njit
1170+
def seed(a):
1171+
random.seed(a)
1172+
1173+
@njit
1174+
def rand():
1175+
return random.random()
1176+
1177+
1178+
# Incorrect seeding
1179+
random.seed(1234)
1180+
print(rand())
1181+
1182+
random.seed(1234)
1183+
print(rand())
1184+
1185+
# Correct seeding
1186+
seed(1234)
1187+
print(rand())
1188+
1189+
seed(1234)
1190+
print(rand())
1191+
11621192
11631193
.. note::
11641194
Since version 0.28.0, the generator is thread-safe and fork-safe. Each

0 commit comments

Comments
 (0)