lundi 27 septembre 2021

Random strange behavior

I've created just for fun this method to find out how long it takes until this random method goes into a loop and what the seed is when this occurs:

public static void main(String[] args) {
      HashMap<Integer,Integer> map = new HashMap<>();
      for (int i = 0; i < 1000; i++) {
        int key = firstRepeatingSeed();
        Integer integer = map.get(key);
        map.put(key, integer == null ? 1 : integer+1);
      }
      System.out.println(map);
    }
  }

  public static int firstRepeatingSeed(){
    Random random = new Random();
    HashSet<Integer> loopDetector = new HashSet<>();
    while (true) {
      int gen = random.nextInt();
      random.setSeed(gen);
      if (!loopDetector.add(gen))
        return gen;
    }
  }

These are the results:

-1381274646=2,
 -1686548232=2,
 154051178=717,
 -381624123=1,
 -334394727=1,
 -1261880000=1,
 -1128634575=1,
 658182704=1,
 364776881=141,
 -1985197764=11,
 1266282155=13,
 -1108864769=1,
 266843583=9,
 -1939453764=2,
 349725186=4,
 1525333558=2,
 -106280330=1,
 -1865148662=1,
 -296326218=1,
 -84817968=2,
 332765684=1,
 -1181949977=1,
 -595175830=59,
 -206288251=1,
 -2043038133=1,
 1220626100=1,
 -541517940=1,
 1373871195=14,
 1890953100=3,
 -1529367891=1,
 -1022666507=3

My question is why does 154051178 and 364776881 show up so often?




Aucun commentaire:

Enregistrer un commentaire