Guest User

Untitled

a guest
Oct 16th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. package pack;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Random;
  6.  
  7. public class CharacterGenerator {
  8. private static Random rand = new Random();
  9. private static final char CHAR_1 = 'a';
  10. private static final char CHAR_2 = 'b';
  11. private static final char CHAR_3 = 'c';
  12.  
  13. public static void main(String[] args) {
  14. Map<Character, Integer> map = new HashMap<>();
  15. for (int i = 0; i < 1_000_000; i++) {
  16. char distribution = distribution();
  17. map.putIfAbsent(distribution, 1);
  18. map.computeIfPresent(distribution, (character, integer) -> ++integer);
  19. }
  20. System.out.println(map);
  21. }
  22.  
  23. public static char distribution() {
  24. while (true) {
  25. int gen1 = gen();
  26. int gen2 = gen();
  27. int gen3 = gen();
  28. if (gen1 == gen2 && gen2 == gen3) {
  29. continue;
  30. }
  31. if (gen1 == gen2) {
  32. return CHAR_1;
  33. } else if (gen2 == gen3) {
  34. return CHAR_2;
  35. } else if (gen3 == gen1) {
  36. return CHAR_3;
  37. }
  38. }
  39. }
  40.  
  41. private static int gen() {
  42. return rand.nextInt(2);
  43. }
  44. }
Add Comment
Please, Sign In to add comment