You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gilles Sadowski (Jira)" <ji...@apache.org> on 2021/06/14 13:12:00 UTC

[jira] [Commented] (RNG-140) nextLong(long lo, long hi)

    [ https://issues.apache.org/jira/browse/RNG-140?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17362947#comment-17362947 ] 

Gilles Sadowski commented on RNG-140:
-------------------------------------

Could the method be added to the {{UniformRandomProvider}} interface and implemented in {{BaseProvider}}?

Code (from Commons Math):
{code:java}
        public long nextLong(final long lower,
                             final long upper) {
            if (lower >= upper) {
                throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                                    lower, upper, false);
            }
            final long max = (upper - lower) + 1;
            if (max <= 0) {
                // Range is too wide to fit in a positive long (larger than 2^63);
                // as it covers more than half the long range, we use directly a
                // simple rejection method.     
                while (true) {
                    final long r = rng.nextLong();
                    if (r >= lower && r <= upper) {
                        return r;
                    }
                }
            } else if (max < Integer.MAX_VALUE){
                // We can shift the range and generate directly a positive int.
                return lower + rng.nextInt((int) max);
            } else {
                // We can shift the range and generate directly a positive long.
                return lower + rng.nextLong(max);
            }
        }
{code}

> nextLong(long lo, long hi)
> --------------------------
>
>                 Key: RNG-140
>                 URL: https://issues.apache.org/jira/browse/RNG-140
>             Project: Commons RNG
>          Issue Type: Wish
>          Components: sampling
>            Reporter: Gilles Sadowski
>            Priority: Minor
>              Labels: api
>
> Replacement for functionality defined in [{{RandomUtils}}|https://gitbox.apache.org/repos/asf?p=commons-math.git;a=blob;f=commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/RandomUtils.java;h=60060e71d5bbe1d00878a1f54f8bb1ff88b65f11;hb=HEAD#l293].



--
This message was sent by Atlassian Jira
(v8.3.4#803005)