commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gilles (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (RNG-33) Consider adding convenience methods to UniformRandomProvider
Date Wed, 01 Feb 2017 23:19:51 GMT

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

Gilles commented on RNG-33:
---------------------------

Here is a way to somewhat alleviate your pain (if you use Java 8).
Given this factory class:
{code}
import java.util.function.IntSupplier;
import java.util.stream.IntStream;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;

public class RngStreamFactory {
    private final UniformRandomProvider rng;

    public RngStreamFactory(RandomSource source) {
        this(source, null);
    }
    public RngStreamFactory(RandomSource source,
                            Object seed) {
        rng = source.create(source, seed);
    }

    public IntStream nextInt() {
        return IntStream.generate(() -> rng.nextInt());
    }
}
{code}
usage would be
{code}
final RngStreamFactory s = new RngStreamFactory(RandomSource.KISS);
final int[] a = s.nextInt().limit(10).toArray();
{code}

If you absolutely need a {{byte\[\]}}, then it is not as straightforward. :(

Can we resolve this issue?

> Consider adding convenience methods to UniformRandomProvider
> ------------------------------------------------------------
>
>                 Key: RNG-33
>                 URL: https://issues.apache.org/jira/browse/RNG-33
>             Project: Commons RNG
>          Issue Type: Improvement
>    Affects Versions: 1.0
>            Reporter: Duncan Jones
>            Priority: Minor
>
> Commons Lang maintains a class called [{{RandomUtils}}|http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/RandomUtils.html],
which supplements the methods offered by {{java.util.Random}}.
> If similar methods were added to {{UniformRandomProvider}}, we could deprecate {{RandomUtils}}
in favour of using RNG.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Mime
View raw message