question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

default_rng.integers(2**32) always return 0

See original GitHub issue

Reproducing code example:

I don’t understand why default_rng.integers(2**32) always return 0, whereas 2**32-1, 2**32+1, 2**40, etc. return random numbers as expected. The result is a numpy.int64, so I expected to be able to generate numbers of up 2**64-1.

$ python3.7 -c 'from numpy.random import default_rng; rng=default_rng(); print([rng.integers(2**32) for _ in range(5)])'
[0, 0, 0, 0, 0]

It’s fine with 2**40:

$ python3.7 -c 'from numpy.random import default_rng; rng=default_rng(); print([rng.integers(2**40) for _ in range(5)])'
[386296210341, 896689857600, 958588149890, 364800985883, 643738305251]

Numpy/Python version information:

I’m running Fedora 31 with python3-3.7.6-2.fc31.x86_64 and python3-numpy-1.17.4-2.fc31.x86_64:

vstinner@apu$ python3.7
Python 3.7.6 (default, Jan 30 2020, 09:44:41) 
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, numpy; print(numpy.__version__, sys.version)
1.17.4 3.7.6 (default, Jan 30 2020, 09:44:41) 
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
WarrenWeckessercommented, Apr 24, 2020

The problem is in master, too:

In [1]: import numpy as np

In [2]: np.__version__
Out[2]: '1.19.0.dev0+6d6df47'

In [3]: rng = np.random.default_rng()

In [4]: rng.integers(2**32, size=8)
Out[4]: array([0, 0, 0, 0, 0, 0, 0, 0])

This was probably introduced in https://github.com/numpy/numpy/pull/14777. I’ll take a look.

1reaction
WarrenWeckessercommented, Apr 25, 2020

It turns out the cause is https://github.com/numpy/numpy/pull/14501, which was backported to 1.17.3. I’m working on a fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.random.Generator.integers — NumPy v1.24 Manual
Return random integers from the “discrete uniform” distribution of the specified dtype. If high is None (the default), then results are from 0...
Read more >
Good Practice in (Pseudo) Random ... - UCL Computer Science
Avoid setting the seeds to zero or small numbers in general – ... This always returns random data immediately, but with a risk...
Read more >
Random-number functions - Stata
Description: uniformly distributed random integer variates on the interval [a, b]. If a or b is nonintegral, runiformint(a,b) returns runiformint(floor(a),.
Read more >
Good Practice in (Pseudo) Random Number ... - StudyLib
Avoid setting the seeds to zero or small numbers in general – try to choose large ... This always returns random data immediately,...
Read more >
Why is (rand() % anything) always 0 in C++? - Stack Overflow
int r = (((double)rand()) / RAND_MAX) * desired_maximum; ... int rand_lim(int limit) { /* return a random number between 0 and limit ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found