Here you can find the source of random(final char[] chars)
Parameter | Description |
---|---|
chars | the sequence of characters |
public static char random(final char[] chars)
//package com.java2s; /**// ww w.j a v a 2 s . c om * Copyright 2013-2016 PayStax, LLC * * Licensed 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.*; public class Main { private static Random rand = new Random(); public static char[] alphaNumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" .toCharArray(); /** * Returns a random character from a sequence of characters. * * @param chars the sequence of characters * @return the random character */ public static char random(final char[] chars) { int idx = rand.nextInt(chars.length); return chars[idx]; } /** * Chooses a random number between min inclusive and max inclusive. * * @param min the minimim number * @param max the maximum number * @return the randon number */ public static int random(int min, int max) { if (min == max) { return 1; } if (min > max) { throw new IllegalArgumentException("max must be greater than or equal to min"); } return rand.nextInt(max - min) + min; } /** * Generates a random string. * * @param min the minimum number of characters allowed * @param max the maximum number of characters allowed * @param chars the characters to choose from or null * @return the random string */ public static String random(final int min, final int max, char[] chars) { if (chars == null) { chars = alphaNumeric; } StringBuilder sb = new StringBuilder(); for (int i = 0; i <= random(min, max); i++) { sb.append(random(chars)); } return sb.toString(); } }