001/*
002 * Licensed to the author under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package de.cuioss.test.generator.internal.net.java.quickcheck.generator.distribution;
018
019import java.util.Random;
020
021/**
022 * The {@link RandomConfiguration} allows to get and set the seed of the random
023 * number generator.
024 * <p>
025 * Setting the seed allows to run test deterministically with the same generated
026 * objects. The {@link #setSeed(long)} method can be used for that.
027 * </p>
028 * <p>
029 * An the other hand the {@link #initSeed()} method sets a new random seed and
030 * return it. This allows to generate random test objects in one run and repeat
031 * the same test by setting the seed with {@link #setSeed(long)}.
032 * </p>
033 * <p>
034 * You can set the seed for the JVM with {@link #SEED_SYSTEM_PROPERTY}. This
035 * system property will be evaluated at start up. Calling {@link #setSeed(long)}
036 * will overwrite this setting.
037 * </p>
038 */
039public final class RandomConfiguration {
040
041    /**
042     * System property ({@code SEED_SYSTEM_PROPERTY}) to set the {@link Long long}
043     * seed value for the random number generator.
044     * <p>
045     * Note: The actual values generated by the RNG still depend on the execution
046     * order. That may be not under the control of quickcheck.
047     * </p>
048     */
049    public static final String SEED_SYSTEM_PROPERTY = "de.cuioss.test.generator.seed";
050    static final Random random = new Random(); // NOSONAR: owolff: This ist not about cryptography,
051                                               // therefore sufficient
052    static long lastSeed = 0L;
053
054    private RandomConfiguration() {
055    }
056
057    static {
058        readSystemProperty();
059    }
060
061    public static long initSeed() {
062        long seed = random.nextLong();
063        setSeed(seed);
064        return seed;
065    }
066
067    public static void setSeed(long seed) {
068        lastSeed = seed;
069        random.setSeed(seed);
070    }
071
072    public static long getLastSeed() {
073        return lastSeed;
074    }
075
076    static void readSystemProperty() {
077        String seed = System.getProperty(SEED_SYSTEM_PROPERTY);
078        if (seed == null)
079            return;
080        setSeed(Long.parseLong(seed));
081    }
082}