Java Random Char randomChar(Random r)

Here you can find the source of randomChar(Random r)

Description

random Char

License

Apache License

Declaration

private static char randomChar(Random r) 

Method Source Code

//package com.java2s;
/*//w  w w . ja  v a 2s.  co m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Random;

public class Main {
    private static char randomChar(Random r) {
        switch (r.nextInt(101) / 100) {
        case 0:
        case 1:
            // 20% ascii
            return (char) (32 + r.nextInt(127 - 32));
        case 2:
        case 3:
        case 4:
        case 5:
            // 40% a-z
            return (char) ('a' + r.nextInt('z' - 'a'));
        case 6:
            // 10% A-Z
            return (char) ('A' + r.nextInt('Z' - 'A'));
        case 7:
        case 8:
            // 20% 0-9
            return (char) ('0' + r.nextInt('9' - '0'));
        case 9:
            // 10% aeiou
            return "aeiou".charAt(r.nextInt("aeiou".length()));
        }
        // 1% unicode
        return (char) r.nextInt(65535);
    }
}

Related

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