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.

cupy.random.uniform(low=low, high=high) returns high

See original GitHub issue
import cupy as cp

cp.random.seed(seed=13)
high = 2**32 - 1
low = 2**32 - 3
size = 100000000
(cp.random.uniform(low=low, high=high, size=size) == high).sum()

returns array(11), while array(0) is expected, as “Samples are drawn from a uniform distribution over the half-open interval [low, high)”.

Interestingly, the documentation of standard random.uniform says “The end-point value b may or may not be included in the range depending on floating-point rounding in the equation a + (b-a) * random()”. RandomState._scale_kernel seems to do the same thing, so it might be a documentation problem.

BTW, the behavior looks like it is consistent with numpy.

  • Conditions
CuPy Version          : 6.0.0
CUDA Root             : /usr/local/cuda
CUDA Build Version    : 10000
CUDA Driver Version   : 10010
CUDA Runtime Version  : 10000
cuDNN Build Version   : 7301
cuDNN Version         : 7605
NCCL Build Version    : 1000
NCCL Runtime Version  : (unknown)
Linux 4.4.180-102-default 
Intel(R) Xeon(R) Gold 5118 CPU @ 2.30GHz
Tesla P100

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
akopichcommented, Oct 23, 2020

@toslunar We’re dealing with the expression round(low+round(r*round(high-low))). Let D denote the largest distance between the two consecutive floats in the interval [low, high]. The change of the rounding policy will shift the value of round(high-low) by at most D. As long as |r|<=1, the value r*round(high-low) will be also shifted by at most D. Hence, round(r*round(high-low)) shall not be shifted by more than 2D and round(low+round(r*round(high-low))) – by 3D. Clearly, the mean will not be shifted by more than 3D either. And it doesn’t strike me as a significant change.

The result with x[x == high] = low (maybe x[x >= high] = low) looks similar to the round-toward-negative…

Why, is it low on RHS? Why not smth like std::nextafter(high, 0.0)?

Thank you for referencing the numpy issue. Looks like they found it possible to just change the rounding mode, but lest they break the others’ code, they just edited the docs.

1reaction
anarusecommented, Oct 19, 2020

Ah, I think I finally figured out what you’re saying is the issue. In numpy.random.uniform, "The high limit may be included in the returned array of floats due to floating-point rounding in the equation low + (high-low) * random_sample(). ". You’re saying that the problem is that there is no such an explanation in the cupy.random.uniform document?

Read more comments on GitHub >

github_iconTop Results From Across the Web

cupy.random.uniform — CuPy 11.4.0 documentation
Returns an array of uniformly-distributed samples over an interval. Samples are drawn from a uniform distribution over the half-open interval [low, high) ....
Read more >
cupy.random.uniform — CuPy 7.8.0 documentation
Returns an array of uniformly-distributed samples over an interval. Samples are drawn from a uniform distribution over the half-open interval [low, high) ....
Read more >
Random sampling (cupy.random) — CuPy 11.4.0 ...
Functions in cupy.random # ; uniform ([low, high, size, dtype]). Returns an array of uniformly-distributed samples over an interval. ; vonmises (mu, kappa[,...
Read more >
Random Sampling (cupy.random) — CuPy 4.5.0 documentation
cupy.random.rand, Returns an array of uniform random values over the interval [0, 1) . ... Returns a scalar or an array of integer...
Read more >
cupy.random.random_integers — CuPy 11.3.0 documentation
Return a scalar or an array of integer values over [low, high] ... are independently sampled from uniform distribution over closed interval [low,...
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