Here you can find the source of getRandomChar(int[][] ranges, char differentThen, boolean caseSensitive, Random random)
Parameter | Description |
---|---|
ranges | the different Ascii ranges for the char - for example: {{67,95},{97,115}} |
differentThen | the char to find replacement for |
caseSensitive | if False will return a char which is also different from the upper and lower case representation of the given char |
ranbdom | the Random object to randomize with |
public static char getRandomChar(int[][] ranges, char differentThen, boolean caseSensitive, Random random)
//package com.java2s; import java.util.ArrayList; import java.util.Random; public class Main { /**//from w w w . j av a 2 s . c o m * Get a random char in given ranges, different than given char * * @param ranges the different Ascii ranges for the char - for example: {{67,95},{97,115}} * @param differentThen the char to find replacement for * @param caseSensitive if False will return a char which is also different * from the upper and lower case representation of the given char * @param ranbdom the Random object to randomize with * @return a random chars in given conditions */ public static char getRandomChar(int[][] ranges, char differentThen, boolean caseSensitive, Random random) { ArrayList<Character> chars = getAllCharsInRange(ranges); if (differentThen != ' ') { if (caseSensitive) { chars.remove(new Character(differentThen)); } else { chars.remove(new Character((char) ((differentThen + "").toUpperCase().charAt(0)))); chars.remove(new Character((char) ((differentThen + "").toLowerCase().charAt(0)))); } } int index = getRandomInt(0, chars.size() - 1, random); return chars.get(index); } /** * Get an arrayList of chars in given ranges * * @param ranges the different Ascii ranges for the char - for example: {{67,95},{97,115}} * @return an ArrayList of all chars in ranges */ public static ArrayList<Character> getAllCharsInRange(int[][] ranges) { ArrayList<Character> chars = new ArrayList<Character>(); for (int[] range : ranges) { for (int i = range[0]; i <= range[1]; i++) { chars.add((char) i); } } return chars; } /** * Get a random int value in given range * * @param min range start * @param max range end * @param random the Random object to random with * @return an int value in the given range */ public static int getRandomInt(int min, int max, Random random) { int diff = max - min + 1; int randomInt = random.nextInt(diff); return randomInt + min; } }