Java Random Char getRandomChar(int[][] ranges, char differentThen, boolean caseSensitive, Random random)

Here you can find the source of getRandomChar(int[][] ranges, char differentThen, boolean caseSensitive, Random random)

Description

Get a random char in given ranges, different than given char

License

Open Source License

Parameter

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

Return

a random chars in given conditions

Declaration

public static char getRandomChar(int[][] ranges, char differentThen, boolean caseSensitive, Random random) 

Method Source Code


//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;
    }
}

Related

  1. getRandomChar()
  2. getRandomChar()
  3. getRandomChar(boolean number, boolean lower, boolean upper, boolean other, String extra, Random _random)
  4. getRandomChar(int size)
  5. getRandomChar(int x)
  6. getRandomChar(Random random, boolean upper)
  7. getRandomChar(Random random, int type)
  8. getRandomCharacter()
  9. getRandomCharacter()