All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.clementlevallois.utils.RandomStringGenerator Maven / Gradle / Ivy

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.clementlevallois.utils;

import java.util.Random;

/**
 *
 * @author LEVALLOIS
 */
public class RandomStringGenerator {

    /**
     *
     * @param length
     * @return
     */
    public static String generate(int length) {
        int leftLimit = 48; // numeral '0'
        int rightLimit = 122; // letter 'z'
        int targetStringLength = length;
        Random random = new Random();

        String generatedString = random.ints(leftLimit, rightLimit + 1)
                .filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
                .limit(targetStringLength)
                .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
                .toString();

        return generatedString;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy