0

I'm writing a signal generator app in Java and one of functionality is to generate a signal which is noise with gaussian distribution. My current implementation for returning values of this class is folowing:

import java.util.Random;

public class FormulaGaussianDistributionNoise extends Formula {

    private Double stdEv;
    private Random r;

    public FormulaGaussianDistributionNoise(Double stdEv) {
        this.stdEv = stdEv;
        this.r = new Random();
    }

    @Override
    public Double value(Double x) {
        return stdEv*r.nextGaussian();
    }
}

When I generate ie. 10secs signal with 100Hz sampling im getting preety nice gaussian noise signal which looks like this: Great gaussian signal

However the problem is that i want my signal to become DETERMINISTIC once generated. As probably many of you noticed, class that I prepared will return two different values if called twice with the same argument. I want to achieve that my method value(Double x) of FormulaGaussianDistributionNoise object called with the same argument returns always the same value.

What I've tried so far was using method setSeed(long x) of the Random class, in each call of the value(Double x). My class then looked like this:

public class FormulaGaussianDistributionNoise extends Formula {

    private Double stdEv;
    private Random r;
    private long seed;

    public FormulaGaussianDistributionNoise(Double stdEv) {
        this.stdEv = stdEv;
        this.seed = System.currentTimeMillis();
        this.r = new Random(seed);
    }

    @Override
    public Double value(Double x) {
        //Generate new seed based on the double value 
        long newSeed = Double.doubleToRawLongBits(x);
        r.setSeed(this.seed ^ newSeed);
        return stdEv*r.nextGaussian();
    }
}

this.seed atribute of the class is used to make objects of this class different between each others. On value(x) call i set the new seed for the Random object as bits xor of this,seed and x. So it always sets the same seed for the r object if called with the same x, so nextGaussian() always returns the same value. Problem is that that actually those values often repeats and signal does not look like gaussian anymore: Great NO-gaussian signal

I've digged a bit deeper into this and implementation of setSeed(long seed) of Random class looks like following:

    /**
     * Sets the seed of this random number generator using a single
     * {@code long} seed. The general contract of {@code setSeed} is
     * that it alters the state of this random number generator object
     * so as to be in exactly the same state as if it had just been
     * created with the argument {@code seed} as a seed. The method
     * {@code setSeed} is implemented by class {@code Random} by
     * atomically updating the seed to
     *  <pre>{@code (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1)}</pre>
     * and clearing the {@code haveNextNextGaussian} flag used by {@link
     * #nextGaussian}.
     *
     * <p>The implementation of {@code setSeed} by class {@code Random}
     * happens to use only 48 bits of the given seed. In general, however,
     * an overriding method may use all 64 bits of the {@code long}
     * argument as a seed value.
     *
     * @param seed the initial seed
     */
    synchronized public void setSeed(long seed) {
        this.seed.set(initialScramble(seed));
        haveNextNextGaussian = false;
    }

    private static long initialScramble(long seed) {
        return (seed ^ multiplier) & mask;
    }

    private static final long multiplier = 0x5DEECE66DL;
    private static final long mask = (1L << 48) - 1;

As you can see method setSeed(long seed) is setting seed as some initialScramble(seed) which actually uses only 48-bits of my 64-bit seed. When i scramble my seeds like that I'm actually often getting the same seed:

    this.seed      x                newSeed   this.seed^newSeed [y] initialScramble(y)
1590082351125   0.0                       0           1590082351125      1614482495096  
1590082351125   0.25    4598175219545276416     4598176809627627541      1614482495096  
1590082351125   0.5     4602678819172646912     4602680409254998037      1614482495096  
1590082351125   0.75    4604930618986332160     4604932209068683285      1614482495096  
1590082351125   1.0     4607182418800017408     4607184008882368533      1614482495096  
1590082351125   1.25    4608308318706860032     4608309908789211157      1614482495096  
1590082351125   1.5     4609434218613702656     4609435808696053781      1614482495096  
1590082351125   1.75    4610560118520545280     4610561708602896405      1614482495096  
1590082351125   2.0     4611686018427387904     4611687608509739029      1614482495096  
1590082351125   2.25    4612248968380809216     4612250558463160341      1614482495096  
1590082351125   2.5     4612811918334230528     4612813508416581653      1614482495096  
1590082351125   2.75    4613374868287651840     4613376458370002965      1614482495096  
1590082351125   3.0     4613937818241073152     4613939408323424277      1614482495096  
1590082351125   3.25    4614500768194494464     4614502358276845589      1614482495096  
1590082351125   3.5     4615063718147915776     4615065308230266901      1614482495096  
1590082351125   3.75    4615626668101337088     4615628258183688213      1614482495096  
1590082351125   4.0     4616189618054758400     4616191208137109525      1614482495096  
1590082351125   4.25    4616471093031469056     4616472683113820181      1614482495096  
1590082351125   4.5     4616752568008179712     4616754158090530837      1614482495096  
1590082351125   4.75    4617034042984890368     4617035633067241493      1614482495096  
1590082351125   5.0     4617315517961601024     4617317108043952149      1614482495096  
1590082351125   5.25    4617596992938311680     4617598583020662805      1614482495096  
1590082351125   5.5     4617878467915022336     4617880057997373461      1614482495096  
1590082351125   5.75    4618159942891732992     4618161532974084117      1614482495096  
1590082351125   6.0     4618441417868443648     4618443007950794773      1614482495096  
1590082351125   6.25    4618722892845154304     4618724482927505429      1614482495096  
1590082351125   6.5     4619004367821864960     4619005957904216085      1614482495096  
1590082351125   6.75    4619285842798575616     4619287432880926741      1614482495096  
1590082351125   7.0     4619567317775286272     4619568907857637397      1614482495096  
1590082351125   7.25    4619848792751996928     4619850382834348053      1614482495096  
1590082351125   7.5     4620130267728707584     4620131857811058709      1614482495096  
1590082351125   7.75    4620411742705418240     4620413332787769365      1614482495096  
1590082351125   8.0     4620693217682128896     4620694807764480021      1614482495096  
1590082351125   8.5     4620974692658839552     4620976282741190677      1614482495096  
1590082351125   9.0     4621256167635550208     4621257757717901333      1614482495096  
1590082351125   9.5     4621537642612260864     4621539232694611989      1614482495096

However overwriting Random class to get rid of initialScramble also didn't help. I find that method Random.next() also uses only 48bits of seed, so this is probably the problem.

   /**
     * Generates the next pseudorandom number. Subclasses should
     * override this, as this is used by all other methods.
     *
     * <p>The general contract of {@code next} is that it returns an
     * {@code int} value and if the argument {@code bits} is between
     * {@code 1} and {@code 32} (inclusive), then that many low-order
     * bits of the returned value will be (approximately) independently
     * chosen bit values, each of which is (approximately) equally
     * likely to be {@code 0} or {@code 1}. The method {@code next} is
     * implemented by class {@code Random} by atomically updating the seed to
     *  <pre>{@code (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)}</pre>
     * and returning
     *  <pre>{@code (int)(seed >>> (48 - bits))}.</pre>
     *
     * This is a linear congruential pseudorandom number generator, as
     * defined by D. H. Lehmer and described by Donald E. Knuth in
     * <i>The Art of Computer Programming,</i> Volume 3:
     * <i>Seminumerical Algorithms</i>, section 3.2.1.
     *
     * @param  bits random bits
     * @return the next pseudorandom value from this random number
     *         generator's sequence
     * @since  1.1
     */
    protected int next(int bits) {
        long oldseed, nextseed;
        AtomicLong seed = this.seed;
        do {
            oldseed = seed.get();
            nextseed = (oldseed * multiplier + addend) & mask;
        } while (!seed.compareAndSet(oldseed, nextseed));
        return (int)(nextseed >>> (48 - bits));
    }

    private static final long multiplier = 0x5DEECE66DL;
    private static final long addend = 0xBL;
    private static final long mask = (1L << 48) - 1;

My questions are: are there Random number generator classes that uses 64 bits of seed, to generate next value? I couldn't find any. Maybe you have idea for different solution to make my value(Double x) deterministic?

theSaint
  • 1
  • 1
  • Either pre-generate the whole series or cache values. – Olivier May 21 '20 at 18:06
  • @Olivier Your solution have two issues: first of all this is a lot of memory consumption, as i usually generate much more values than just presented 1k. 2nd: I'm also serializing this class, the way you proposed could again generate different values after each deserialization. – theSaint May 21 '20 at 19:46
  • 1
    Can't you just save the seed? With it, you will be able to regenerate the same series. – Olivier May 21 '20 at 20:53
  • Only assuming exactly same sampling - if I start sampling with different frequency or start point i can get different values for the same `x` values. – theSaint May 21 '20 at 23:35

1 Answers1

0

I have achived desired effect by using SecureRandom class from java.security package. Answear for this post helped me : How to generate all possible 64 bit random values in java?

Working implementation:

import java.security.SecureRandom;

public class FormulaGaussianDistributionNoise extends Formula {

    private Double stdEv;
    private long basicSeed;

    public FormulaGaussianDistributionNoise(Double stdEv) {
        this.stdEv = stdEv;
        this.basicSeed = System.currentTimeMillis();
    }

    @Override
    public Double value(Double x) {
        SecureRandom r = new SecureRandom();
        long newSeed = Double.doubleToRawLongBits(x);
        r.setSeed(this.basicSeed ^ newSeed);

        return stdEv*r.nextGaussian();
    }
}
theSaint
  • 1
  • 1