@Test public void testRandomByWeight() { List<TestData> data = new ArrayList<>(); TestData e = new TestData(); e.setId(1); e.setWeight(RandomUtils.nextInt(100)); data.add(e); TestData random = RandomUtils.randomByWeight(data, TestData::getWeight); assertTrue(random.getId() == e.getId()); }
public static <K> K randomByWeight(Map<K, Integer> data) { final int sum = data.values().stream().reduce(0, (a, b) -> a + b); if (sum <= 0) { return randomList(new ArrayList<>(data.keySet())); } final int random = nextInt(sum); int step = 0; for (Map.Entry<K, Integer> e : data.entrySet()) { step += e.getValue().intValue(); if (step > random) { return e.getKey(); } } throw new RuntimeException("randomByWeight的实现有Bug：" + random); }
RandomUtils { public static <K> K randomByWeight(Map<K, Integer> data) { final int sum = data.values().stream().reduce(0, (a, b) -> a + b); if (sum <= 0) { return randomList(new ArrayList<>(data.keySet())); } final int random = nextInt(sum); int step = 0; for (Map.Entry<K, Integer> e : data.entrySet()) { step += e.getValue().intValue(); if (step > random) { return e.getKey(); } } throw new RuntimeException("randomByWeight的实现有Bug：" + random); } }
RandomUtils { public static <K> K randomByWeight(Map<K, Integer> data) { final int sum = data.values().stream().reduce(0, (a, b) -> a + b); if (sum <= 0) { return randomList(new ArrayList<>(data.keySet())); } final int random = nextInt(sum); int step = 0; for (Map.Entry<K, Integer> e : data.entrySet()) { step += e.getValue().intValue(); if (step > random) { return e.getKey(); } } throw new RuntimeException("randomByWeight的实现有Bug：" + random); }  }
RandomUtils { public static <K> K randomByWeight(Map<K, Integer> data) { final int sum = data.values().stream().reduce(0, (a, b) -> a + b); if (sum <= 0) { return randomList(new ArrayList<>(data.keySet())); } final int random = nextInt(sum); int step = 0; for (Map.Entry<K, Integer> e : data.entrySet()) { step += e.getValue().intValue(); if (step > random) { return e.getKey(); } } throw new RuntimeException("randomByWeight的实现有Bug：" + random); }  static boolean nextBoolean(); static int nextInt(int bound); static int nextInt(int origin, int bound); static long nextLong(long bound); static long nextLong(long origin, long bound); static boolean isSuccess(float rate); static boolean isSuccess(double rate); static boolean isSuccessByPercentage(long rate); static boolean isSuccessByPermillage(long rate); static T randomList(List<T> list); @SuppressWarnings("unchecked") static List<T> randomList(final List<T> source, int num); static K randomByWeight(Map<K, Integer> data); static T randomByWeight(List<T> data, ToIntFunction<? super T> weightFunction); static List<T> randomByWeight(List<T> data, ToIntFunction<? super T> weightFunction, int num); }
RandomUtils { public static <K> K randomByWeight(Map<K, Integer> data) { final int sum = data.values().stream().reduce(0, (a, b) -> a + b); if (sum <= 0) { return randomList(new ArrayList<>(data.keySet())); } final int random = nextInt(sum); int step = 0; for (Map.Entry<K, Integer> e : data.entrySet()) { step += e.getValue().intValue(); if (step > random) { return e.getKey(); } } throw new RuntimeException("randomByWeight的实现有Bug：" + random); }  static boolean nextBoolean(); static int nextInt(int bound); static int nextInt(int origin, int bound); static long nextLong(long bound); static long nextLong(long origin, long bound); static boolean isSuccess(float rate); static boolean isSuccess(double rate); static boolean isSuccessByPercentage(long rate); static boolean isSuccessByPermillage(long rate); static T randomList(List<T> list); @SuppressWarnings("unchecked") static List<T> randomList(final List<T> source, int num); static K randomByWeight(Map<K, Integer> data); static T randomByWeight(List<T> data, ToIntFunction<? super T> weightFunction); static List<T> randomByWeight(List<T> data, ToIntFunction<? super T> weightFunction, int num);  }