Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pack;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Random;
- public class CharacterGenerator {
- private static Random rand = new Random();
- private static final char CHAR_1 = 'a';
- private static final char CHAR_2 = 'b';
- private static final char CHAR_3 = 'c';
- public static void main(String[] args) {
- Map<Character, Integer> map = new HashMap<>();
- for (int i = 0; i < 1_000_000; i++) {
- char distribution = distribution();
- map.putIfAbsent(distribution, 1);
- map.computeIfPresent(distribution, (character, integer) -> ++integer);
- }
- System.out.println(map);
- }
- public static char distribution() {
- while (true) {
- int gen1 = gen();
- int gen2 = gen();
- int gen3 = gen();
- if (gen1 == gen2 && gen2 == gen3) {
- continue;
- }
- if (gen1 == gen2) {
- return CHAR_1;
- } else if (gen2 == gen3) {
- return CHAR_2;
- } else if (gen3 == gen1) {
- return CHAR_3;
- }
- }
- }
- private static int gen() {
- return rand.nextInt(2);
- }
- }
Add Comment
Please, Sign In to add comment