Here you can find the source of getRandomString(int cantidad, boolean mayusculas, boolean minusculas, boolean numeros, boolean simbolos, boolean repetir)
Parameter | Description |
---|---|
cantidad | a parameter |
mayusculas | a parameter |
minusculas | a parameter |
numeros | a parameter |
simbolos | a parameter |
repetir | a parameter |
public static String getRandomString(int cantidad, boolean mayusculas, boolean minusculas, boolean numeros, boolean simbolos, boolean repetir)
//package com.java2s; /*/*from w w w . j a va 2 s . c om*/ * Copyright 2012 GBSYS. * * 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.List; import java.util.Random; public class Main { private static Random RANDOM = new Random(System.nanoTime()); /** * * @param cantidad * @param mayusculas * @param minusculas * @param numeros * @param simbolos * @param repetir * @return */ public static String getRandomString(int cantidad, boolean mayusculas, boolean minusculas, boolean numeros, boolean simbolos, boolean repetir) { String vNumeros = "0123456789"; String vMayusculas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String vMinusculas = "abcdefghijklmnopqrstuvwxyz"; String vSimbolos = "!@#$%&*-_+,./|:;()[]{}<>?= "; StringBuilder busqueda = new StringBuilder(); if (mayusculas) { busqueda.append(vMayusculas); } if (minusculas) { busqueda.append(vMinusculas); } if (numeros) { busqueda.append(vNumeros); } if (simbolos) { busqueda.append(vSimbolos); } return getRandomString(cantidad, busqueda.toString(), repetir); } /** * * @param cantidad * @param caracteres * @param repetir * @return */ public static String getRandomString(int cantidad, String caracteres, boolean repetir) { StringBuilder sb = new StringBuilder(cantidad); int longitud = caracteres.length(); do { char c = caracteres.charAt(getRandom(0, longitud)); if (!repetir) { if (sb.toString().contains(String.valueOf(c))) { continue; } } sb.append(c); } while (sb.length() < cantidad); return sb.toString(); } public static int getRandom(final int pMin, final int pMax) { // return (int) (pMin + RANDOM.nextDouble() * (pMax - pMin)); // return RANDOM.nextInt(pMax) + pMin; // return RANDOM.nextInt(pMax - pMin + 1) + pMin; // return pMin + (int) (Math.random() * pMax); // return pMin + (int)(Math.random() * ((pMax - pMin) + 1)); //http://stackoverflow.com/questions/363681/java-generating-random-number-in-a-range if (pMin > pMax) { throw new IllegalArgumentException("Rango invalido [" + pMin + ", " + pMax + "]."); } int diferencia = pMax - pMin; if (diferencia >= 0 && diferencia != Integer.MAX_VALUE) { return (pMin + RANDOM.nextInt(diferencia + 1)); } int aleatorio; do { aleatorio = RANDOM.nextInt(); } while (aleatorio < pMin || aleatorio > pMax); return aleatorio; } public static float getRandom(final float pMin, final float pMax) { return pMin + RANDOM.nextFloat() * (pMax - pMin); } public static double getRandom(final double pMin, final double pMax) { return pMin + RANDOM.nextDouble() * (pMax - pMin); } public static long getRandom(final long pMin, final long pMax) { return pMin + RANDOM.nextLong() * (pMax - pMin); } public static boolean getRandom() { return RANDOM.nextBoolean(); } /** * Obtiene una lista con cantidad elementos ordenador aleatoriamente, de otra lista * @param <T> * @param lista * @param cantidad * @param repetir * @return */ public static <T> List<T> getRandom(List<T> lista, long cantidad, boolean repetir) { return null; } }