Java Random Char getRandomChar(boolean number, boolean lower, boolean upper, boolean other, String extra, Random _random)

Here you can find the source of getRandomChar(boolean number, boolean lower, boolean upper, boolean other, String extra, Random _random)

Description

Von http://www.jswelt.de/showsource.php?id=1123706440

License

Open Source License

Parameter

Parameter Description
number a parameter
lower a parameter
upper a parameter
other a parameter
extra a parameter
_random a parameter

Declaration

public static char getRandomChar(boolean number, boolean lower, boolean upper, boolean other, String extra,
        Random _random) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Random;

public class Main {
    public final static String numberCharacters = "0123456789";
    public final static String lowerCharacters = "abcdefghijklmnopqrstuvwxyz";
    public final static String upperCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public final static String otherCharacters = "%_!?";

    /**//  w  w w  . j av a  2 s .c  o  m
     * Von http://www.jswelt.de/showsource.php?id=1123706440
     * 
     * @param number
     * @param lower
     * @param upper
     * @param other
     * @param extra
     * @param _random
     * @return
     */
    public static char getRandomChar(boolean number, boolean lower, boolean upper, boolean other, String extra,
            Random _random) {
        String charSet = extra;
        if (number == true)
            charSet += numberCharacters;
        if (lower == true)
            charSet += lowerCharacters;
        if (upper == true)
            charSet += upperCharacters;
        if (other == true)
            charSet += otherCharacters;
        return charSet.charAt(generateRandomInteger(0, (charSet.length() - 1), _random));
    }

    /**
     * Erzeugt eine Zufallszahl
     * 
     * @param aStart
     * @param aEnd
     * @param aRandom
     * @return
     */
    public static int generateRandomInteger(int aStart, int aEnd, Random aRandom) {
        if (aStart > aEnd) {
            throw new IllegalArgumentException("Start cannot exceed End.");
        }
        // get the range, casting to long to avoid overflow problems
        long range = (long) aEnd - (long) aStart + 1;
        // compute a fraction of the range, 0 <= frac < range
        long fraction = (long) (range * aRandom.nextDouble());
        int randomNumber = (int) (fraction + aStart);

        return randomNumber;
    }
}

Related

  1. createRandomChar()
  2. getRandomChar()
  3. getRandomChar()
  4. getRandomChar()
  5. getRandomChar(int size)
  6. getRandomChar(int x)
  7. getRandomChar(int[][] ranges, char differentThen, boolean caseSensitive, Random random)
  8. getRandomChar(Random random, boolean upper)