Java Code Examples for org.apache.commons.rng.UniformRandomProvider#toString()

The following examples show how to use org.apache.commons.rng.UniformRandomProvider#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RNGUtils.java    From commons-rng with Apache License 2.0 6 votes vote down vote up
/**
 * Wrap the random generator with an {@link IntProvider} that will use
 * {@link UniformRandomProvider#nextInt()}.
 * An input {@link RandomIntSource} is returned unmodified.
 *
 * @param rng The random generator.
 * @return the int random generator.
 */
static UniformRandomProvider createIntProvider(final UniformRandomProvider rng) {
    if (!(rng instanceof RandomIntSource)) {
        return new IntProvider() {
            @Override
            public int next() {
                return rng.nextInt();
            }

            @Override
            public String toString() {
                return "Int bits " + rng.toString();
            }
        };
    }
    return rng;
}
 
Example 2
Source File: RNGUtils.java    From commons-rng with Apache License 2.0 6 votes vote down vote up
/**
 * Wrap the random generator with an {@link IntProvider} that will use the upper
 * 32-bits of the {@code long} from {@link UniformRandomProvider#nextLong()}.
 * The input must be a {@link RandomLongSource}.
 *
 * @param rng The random generator.
 * @return the upper bits random generator.
 * @throws ApplicationException If the input source native type is not 64-bit.
 */
static UniformRandomProvider createLongUpperBitsIntProvider(final UniformRandomProvider rng) {
    if (rng instanceof RandomLongSource) {
        return new IntProvider() {
            @Override
            public int next() {
                return (int) (rng.nextLong() >>> 32);
            }

            @Override
            public String toString() {
                return "Long upper-bits " + rng.toString();
            }
        };
    }
    throw new ApplicationException(NOT_LONG_SOURCE + rng);
}
 
Example 3
Source File: RNGUtils.java    From commons-rng with Apache License 2.0 6 votes vote down vote up
/**
 * Wrap the random generator with an {@link IntProvider} that will use the lower
 * 32-bits of the {@code long} from {@link UniformRandomProvider#nextLong()}.
 * The input must be a {@link RandomLongSource}.
 *
 * @param rng The random generator.
 * @return the lower bits random generator.
 * @throws ApplicationException If the input source native type is not 64-bit.
 */
static UniformRandomProvider createLongLowerBitsIntProvider(final UniformRandomProvider rng) {
    if (rng instanceof RandomLongSource) {
        return new IntProvider() {
            @Override
            public int next() {
                return (int) rng.nextLong();
            }

            @Override
            public String toString() {
                return "Long lower-bits " + rng.toString();
            }
        };
    }
    throw new ApplicationException(NOT_LONG_SOURCE + rng);
}
 
Example 4
Source File: RNGUtils.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Wrap the random generator with a new instance that will reverse the byte order of
 * the native type. The input must be either a {@link RandomIntSource} or
 * {@link RandomLongSource}.
 *
 * @param rng The random generator.
 * @return the byte reversed random generator.
 * @throws ApplicationException If the input source native type is not recognised.
 * @see Integer#reverseBytes(int)
 * @see Long#reverseBytes(long)
 */
static UniformRandomProvider createReverseBytesProvider(final UniformRandomProvider rng) {
    if (rng instanceof RandomIntSource) {
        return new IntProvider() {
            @Override
            public int next() {
                return Integer.reverseBytes(rng.nextInt());
            }

            @Override
            public String toString() {
                return BYTE_REVERSED + rng.toString();
            }
        };
    }
    if (rng instanceof RandomLongSource) {
        return new LongProvider() {
            @Override
            public long next() {
                return Long.reverseBytes(rng.nextLong());
            }

            @Override
            public String toString() {
                return BYTE_REVERSED + rng.toString();
            }
        };
    }
    throw new ApplicationException(UNRECOGNISED_NATIVE_TYPE + rng);
}
 
Example 5
Source File: RNGUtils.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Wrap the random generator with a new instance that will reverse the bits of
 * the native type. The input must be either a {@link RandomIntSource} or
 * {@link RandomLongSource}.
 *
 * @param rng The random generator.
 * @return the bit reversed random generator.
 * @throws ApplicationException If the input source native type is not recognised.
 * @see Integer#reverse(int)
 * @see Long#reverse(long)
 */
static UniformRandomProvider createReverseBitsProvider(final UniformRandomProvider rng) {
    if (rng instanceof RandomIntSource) {
        return new IntProvider() {
            @Override
            public int next() {
                return Integer.reverse(rng.nextInt());
            }

            @Override
            public String toString() {
                return BIT_REVERSED + rng.toString();
            }
        };
    }
    if (rng instanceof RandomLongSource) {
        return new LongProvider() {
            @Override
            public long next() {
                return Long.reverse(rng.nextLong());
            }

            @Override
            public String toString() {
                return BIT_REVERSED + rng.toString();
            }
        };
    }
    throw new ApplicationException(UNRECOGNISED_NATIVE_TYPE + rng);
}
 
Example 6
Source File: RNGUtils.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Wrap the random generator with a new instance that will combine the bits
 * using a {@code xor} operation with a generated hash code. The input must be either
 * a {@link RandomIntSource} or {@link RandomLongSource}.
 *
 * <pre>
 * {@code
 * System.identityHashCode(new Object()) ^ rng.nextInt()
 * }
 * </pre>
 *
 * Note: This generator will be slow.
 *
 * @param rng The random generator.
 * @return the combined random generator.
 * @throws ApplicationException If the input source native type is not recognised.
 * @see System#identityHashCode(Object)
 */
static UniformRandomProvider createHashCodeProvider(final UniformRandomProvider rng) {
    if (rng instanceof RandomIntSource) {
        return new IntProvider() {
            @Override
            public int next() {
                return System.identityHashCode(new Object()) ^ rng.nextInt();
            }

            @Override
            public String toString() {
                return HASH_CODE + rng.toString();
            }
        };
    }
    if (rng instanceof RandomLongSource) {
        return new LongProvider() {
            @Override
            public long next() {
                final long mix = NumberFactory.makeLong(System.identityHashCode(new Object()),
                                                        System.identityHashCode(new Object()));
                return mix ^ rng.nextLong();
            }

            @Override
            public String toString() {
                return HASH_CODE + rng.toString();
            }
        };
    }
    throw new ApplicationException(UNRECOGNISED_NATIVE_TYPE + rng);
}
 
Example 7
Source File: RNGUtils.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Wrap the random generator with a new instance that will combine the bits
 * using a {@code xor} operation with the output from {@link ThreadLocalRandom}.
 * The input must be either a {@link RandomIntSource} or {@link RandomLongSource}.
 *
 * <pre>
 * {@code
 * ThreadLocalRandom.current().nextInt() ^ rng.nextInt()
 * }
 * </pre>
 *
 * @param rng The random generator.
 * @return the combined random generator.
 * @throws ApplicationException If the input source native type is not recognised.
 */
static UniformRandomProvider createThreadLocalRandomProvider(final UniformRandomProvider rng) {
    if (rng instanceof RandomIntSource) {
        return new IntProvider() {
            @Override
            public int next() {
                return ThreadLocalRandom.current().nextInt() ^ rng.nextInt();
            }

            @Override
            public String toString() {
                return TLR_MIXED + rng.toString();
            }
        };
    }
    if (rng instanceof RandomLongSource) {
        return new LongProvider() {
            @Override
            public long next() {
                return ThreadLocalRandom.current().nextLong() ^ rng.nextLong();
            }

            @Override
            public String toString() {
                return TLR_MIXED + rng.toString();
            }
        };
    }
    throw new ApplicationException(UNRECOGNISED_NATIVE_TYPE + rng);
}
 
Example 8
Source File: RNGUtils.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Combine the two random generators using a {@code xor} operations.
 * The input must be either a {@link RandomIntSource} or {@link RandomLongSource}.
 * The returned type will match the native output type of {@code rng1}.
 *
 * <pre>
 * {@code
 * rng1.nextInt() ^ rng2.nextInt()
 * }
 * </pre>
 *
 * @param rng1 The first random generator.
 * @param rng2 The second random generator.
 * @return the combined random generator.
 * @throws ApplicationException If the input source native type is not recognised.
 */
static UniformRandomProvider createXorProvider(final UniformRandomProvider rng1,
    final UniformRandomProvider rng2) {
    if (rng1 instanceof RandomIntSource) {
        return new IntProvider() {
            @Override
            public int next() {
                return rng1.nextInt() ^ rng2.nextInt();
            }

            @Override
            public String toString() {
                return rng1.toString() + XOR + rng2.toString();
            }
        };
    }
    if (rng1 instanceof RandomLongSource) {
        return new LongProvider() {
            @Override
            public long next() {
                return rng1.nextLong() ^ rng2.nextLong();
            }

            @Override
            public String toString() {
                return rng1.toString() + XOR + rng2.toString();
            }
        };
    }
    throw new ApplicationException(UNRECOGNISED_NATIVE_TYPE + rng1);
}