Copy instance variable of type java.util.Random to create object in same state

前端 未结 3 1763
轮回少年
轮回少年 2021-01-20 05:58

I\'m implementing a simulated annealing (SA) algorithm, where I need to copy states (e. g. to remember best solution so far).

I implemented a copy method, since it\'

相关标签:
3条回答
  • 2021-01-20 06:24

    I know this is an old question with an accepted answer, but I came across this while looking for an answer myself, and I wound up taking a different approach.

    Seeing mike's note above that Random implements Serializable, I just used that to make the copy:

    /**
     * Uses serialization to create a copy of the given Random, needed for
     * repeatability in some tests.
     */
    public static Random cloneRandom(Random src) throws Exception {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bo);
        oos.writeObject(src);
        oos.close();
        ObjectInputStream ois = new ObjectInputStream(
                new ByteArrayInputStream(bo.toByteArray()));
        return (Random)(ois.readObject());
    }
    

    It probably doesn't perform as well as mike's CopyableRandom, but it's a lot simpler, and sufficent for my little unit test.

    (I had an existing unit test which started with a Random with a known seed & then performed a series of operations; I was trying to add a bit in the middle of the test, and wanted a copy of the Random; calling nextLong() or similar in the middle of the test to get the/a seed was going to change the seed, blowing up the rest of the test. Really I just wanted something like Random.getSeed().)

    0 讨论(0)
  • 2021-01-20 06:28

    I think you should store in your State class, not only starting seed but also number of calls to nextInt() you already did. This is due to the intrisic fact that Random generates pseudo-random sequence of numbers. That is:

    A pseudo random number generator can be started from an arbitrary starting state using a seed state. It will always produce the same sequence thereafter when initialized with that state

    Let me explain show you with a sample first:

    public static void main(String[] args){
    
         Random r = new Random(42);      
         Random s = new Random(42);
    
         for(int i = 0; i < 5; i++){
           System.out.println("First random " +r.nextInt());
         }
    
         for(int i = 0; i < 5; i++){
           System.out.println("Second random " +s.nextInt());
         }
    
      }
    

    which result is:

    First random -1170105035
    First random 234785527
    First random -1360544799
    First random 205897768
    First random 1325939940
    Second random -1170105035
    Second random 234785527
    Second random -1360544799
    Second random 205897768
    Second random 1325939940
    

    Since both Random instances start with same seed, I always get the same sequence of numbers.

    So, when copying object you should init the new Random to the same seed of source (you already do this) and "consume" as much calls of nextInt() that you already used in source object (this is why you have to keep that number).

    After done this, calling the nextInt() on the copy will give you the same result that source object. I hope my code is enough to refactor yours and let you understand my solution.

    To understand better pseudorandom number generators (PRNG), also known as a deterministic random bit generators (DRBG), you can look at this Wikipedia article

    0 讨论(0)
  • 2021-01-20 06:48

    I came up with an own solution. It mainly overrides next() in Random (since all other methods rely on that one), and some other stuff to keep the consistency.

    It delivers an exact copy of the instance this method was invoked on (whether it makes sense to make a copy of a random instance is another topic...^^). It should exactly behave like its super class, at least that was my intention.

    Feel free to add your thoughts!

    Since other questions were about getting the seed: One could easily add a getSeed() method to my solution. Or getInitialSeed(), getCurrentSeed().

    /* Bounded parameter type since a class that implements this interface
     * should only be able to create copies of the same type (or a subtype).
     */
    public interface Copyable<T extends Copyable<T>>
    {
      public T copy();
    }
    

    public class CopyableRandom extends Random implements Copyable<CopyableRandom>
    {
      private final AtomicLong seed = new AtomicLong(0L);
    
      private final static long multiplier = 0x5DEECE66DL;
      private final static long addend = 0xBL;
      private final static long mask = (1L << 48) - 1;
    
      public CopyableRandom() { this(++seedUniquifier + System.nanoTime()); }
      private static volatile long seedUniquifier = 8682522807148012L;
    
      public CopyableRandom(long seed) { this.seed.set((seed ^ multiplier) & mask); }
    
      /* copy of superclasses code, as you can seed the seed changes */
      @Override
      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));
      }
    
      /* necessary to prevent changes to seed that are made in constructor */
      @Override
      public CopyableRandom copy() { return new CopyableRandom((seed.get() ^ multiplier) & mask); }
    
      public static void main(String[] args)
      {
        CopyableRandom cr = new CopyableRandom();
    
        /* changes intern state of cr */
        for (int i = 0; i < 10; i++)
          System.out.println(cr.nextInt(50));
    
        Random copy = cr.copy()
    
        System.out.println("\nTEST: INTEGER\n");
        for (int i = 0; i < 10; i++)
          System.out.println("CR\t= " + cr.nextInt(50) + "\nCOPY\t= " + copy.nextInt(50) + "\n");
    
        Random anotherCopy = (copy instanceof CopyableRandom) ? ((CopyableRandom) copy).copy() : new Random();
        System.out.println("\nTEST: DOUBLE\n");
        for (int i = 0; i < 10; i++)
          System.out.println("CR\t= " + cr.nextDouble() + "\nA_COPY\t= " + anotherCopy.nextDouble() + "\n");
      }
    }
    

    And here the output of the main method:

    19
    23
    26
    37
    41
    34
    17
    28
    29
    6
    
    TEST: INTEGER
    
    CR      = 3
    COPY    = 3
    
    CR      = 18
    COPY    = 18
    
    CR      = 25
    COPY    = 25
    
    CR      = 9
    COPY    = 9
    
    CR      = 24
    COPY    = 24
    
    CR      = 5
    COPY    = 5
    
    CR      = 15
    COPY    = 15
    
    CR      = 5
    COPY    = 5
    
    CR      = 30
    COPY    = 30
    
    CR      = 26
    COPY    = 26
    
    
    TEST: DOUBLE
    
    CR      = 0.7161924830704971
    A_COPY  = 0.7161924830704971
    
    CR      = 0.06333509362539957
    A_COPY  = 0.06333509362539957
    
    CR      = 0.6340753697524675
    A_COPY  = 0.6340753697524675
    
    CR      = 0.13546677259518425
    A_COPY  = 0.13546677259518425
    
    CR      = 0.37133033932410586
    A_COPY  = 0.37133033932410586
    
    CR      = 0.796277965335522
    A_COPY  = 0.796277965335522
    
    CR      = 0.8610310118615391
    A_COPY  = 0.8610310118615391
    
    CR      = 0.793617231340077
    A_COPY  = 0.793617231340077
    
    CR      = 0.3454111197621874
    A_COPY  = 0.3454111197621874
    
    CR      = 0.25314618087856255
    A_COPY  = 0.25314618087856255
    

    I also had a test where I compared CopyableRandom against Random. It yielded the same results.

    long seed = System.nanoTime();
    
    Random cr  = new CopyableRandom(seed);
    Random cmp = new Random(seed);
    
    0 讨论(0)
提交回复
热议问题