Java Random Char randomChar()

Here you can find the source of randomChar()

Description

random Char

License

Apache License

Declaration

public static char randomChar() 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Random;

public class Main {
    private static final char[] CHARS = new char[] { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's',
            'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm' };
    private static Random random;

    public static char randomChar() {
        int len = uniform(0, CHARS.length);
        return CHARS[len];
    }/*from  w w w. ja  va2 s .  c  om*/

    public static double uniform() {
        return random.nextDouble();
    }

    public static int uniform(int n) {
        if (n <= 0) {
            throw new IllegalArgumentException("argument must be positive");
        }
        return random.nextInt(n);
    }

    public static int uniform(int a, int b) {
        if ((b <= a) || ((long) b - a >= Integer.MAX_VALUE)) {
            throw new IllegalArgumentException("invalid range: [" + a + ", " + b + "]");
        }

        return a + uniform(b - a);
    }

    public static double uniform(double a, double b) {
        if (!(a < b)) {
            throw new IllegalArgumentException("invalid range: [" + a + ", " + b + "]");
        }
        return a + uniform() * (b - a);
    }
}

Related

  1. getRandomCharacter()
  2. randomChar()
  3. randomChar()
  4. randomChar()
  5. randomChar()
  6. randomChar(char[] letters)
  7. randomChar(Random r)
  8. randomCharacter(boolean includeSymbols)
  9. randomChars(char[] alphabet, int length)