package fr.umlv.conc;


import java.util.HashSet;

public class RandomNumberGenerator {
    private long x;
    
    public RandomNumberGenerator(long seed) {
        if (seed == 0) {
            throw new IllegalArgumentException("seed == 0");
        }
        x = seed;
    }
    
    public long next() {  // Marsaglia's XorShift
        x ^= x >>> 12;
        x ^= x << 25;
        x ^= x >>> 27;
        return x * 2685821657736338717L;
    }
    
    public static void main(String[] args) throws InterruptedException {
        HashSet<Long> set1 = new HashSet<>();
        HashSet<Long> set2 = new HashSet<>();
        RandomNumberGenerator rng = new RandomNumberGenerator(1);
        Thread t = new Thread(() -> {
            for (int i = 0; i < 5_000; i++) {
                set1.add(rng.next());
            }
        });
        t.start();
        for (int i = 0; i < 5_000; i++) {
            set2.add(rng.next());
        }
        t.join();
        
        System.out.println("set1: " + set1.size() + ", set2: " + set2.size());
        set1.addAll(set2);
        System.out.println("union: " + set1.size());
        // if the RandomNumberGenerator is thread-safe the two sets should be disjoint
        // and the size of the union should be 10_000 the sum of the two sizes
    }
    
}
