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

Alex D Herbert commented on RNG-57:
-----------------------------------

{quote}I don't understand the purpose of a CachedUniformRandomProvider interface
{quote}
It was a class private marker interface. ;)

I showed the method in case you could spot something silly. I am going to think 
about it and try some variants.

Anyway the private classes implemented the marker interface to avoid double 
wrapping. Here is the public API:
{code:java}
/**
 * Factory class for wrapping instances of {@link UniformRandomProvider} to 
cache
 * values that can be reused to provision the interface methods.
 *
 * <p>The int values generated by an {@link IntProvider} can be cached to enable
 * fast provision of {@link UniformRandomProvider#nextBoolean()}.

 * <p>The long values generated by a {@link LongProvider} can be cached to 
enable
 * fast provision of {@link UniformRandomProvider#nextBoolean()} and
 * {@link UniformRandomProvider#nextInt()}.
 */
public final class CachedUniformRandomProviderFactory {

   // PRIVATE STUFF

    /**
     * Wrap the source of randomness.
     *
     * <p>The returned provider will cache values from an {@link IntProvider} or
     * {@link LongProvider} to enable fast provision of
     * {@link UniformRandomProvider#nextBoolean()},
     * and in the case of a {@link LongProvider} also
     * {@link UniformRandomProvider#nextInt()}.
     *
     * <p>If the source of randomness cannot be wrapped then it is returned 
unmodified.
     *
     * @param rng the source of randomness
     * @return the wrapped uniform random provider
     */
    public static UniformRandomProvider wrap(UniformRandomProvider rng) {
        // Avoid double wrapping
        if (rng instanceof CachedUniformRandomProvider) {
            return rng;
        }
        if (rng instanceof LongProvider) {
            return new CachedLongProvider((LongProvider)rng);
        }
        if (rng instanceof IntProvider) {
            return new CachedIntProvider((IntProvider)rng);
        }
        // Unknown implementation
        return rng;
    }
}
{code}
Anyway the API details can be sorted. My aim was to see if I could make it work.

Currently the {{LongProvider}} uses bit masking on longs. However the results 
appear to show the {{nextInt}} is faster or equal for all {{LongProviders}}. So 
I'm going to test a version that uses {{int}} bit masking for the 
{{nextBoolean}}, having generated an {{int}} using a cached {{long}}.

> CachedUniformRandomProvider for nextBoolean() and nextInt()
> -----------------------------------------------------------
>
>                 Key: RNG-57
>                 URL: https://issues.apache.org/jira/browse/RNG-57
>             Project: Commons RNG
>          Issue Type: Improvement
>          Components: sampling
>    Affects Versions: 1.2
>            Reporter: Alex D Herbert
>            Priority: Minor
>              Labels: performance
>
> Implement a wrapper around a {{UniformRandomProvider}} that can cache the 
> underlying source of random bytes for use in the methods {{nextBoolean()}} 
> and {{nextInt()}} (in the case of {{LongProvider}}). E.g.
> {code:java}
> LongProvider provider = RandomSource.create(RandomSource.SPLIT_MIX_64);
> CachedLongProvider rng = new CachedLongProvider(provider);
> // Uses cached nextLong() 64 times
> rng.nextBoolean();
> // Uses cached nextLong() twice
> rng.nextInt();
> IntProvider provider = RandomSource.create(RandomSource.KISS);
> CachedIntProvider rng2 = new CachedIntProvider(provider);
> // Uses cached nextInt() 32 times
> rng2.nextBoolean();
> // This could be wrapped by a factory method:
> UniformRandomProvider rng = CachedUniformRandomProviderFactory.wrap(
>         // Any supported source: IntProvider or LongProvider
>         RandomSource.create(RandomSource...));
> {code}
> The implementation should be speed tested to determine the benefit for 
> {{nextBoolean()}} and if {{nextInt()}} can be improved for {{LongProviders}}.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to