get Random Name By Length - Java java.util

Java examples for java.util:Random

Description

get Random Name By Length

Demo Code


//package com.java2s;

import java.util.Random;

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

    public static String getRandomNameByLength(int length) {
        StringBuilder newRandom = new StringBuilder();
        Random rd = new Random();

        for (int i = 0; i < length; i++) {
            newRandom.append(seed1[rd.nextInt(seed1.length)]);
        }//from ww w .  j  a v  a  2s . com

        return newRandom.toString();
    }

    public static int nextInt() {
        Random random = new Random();
        return random.nextInt();
    }

    public static int nextInt(int n) {
        Random random = new Random();
        return random.nextInt(n);
    }

    public static int nextInt(int min, int max) {
        if (min == max) {
            return min;
        }
        Random random = new Random();
        if (min > max) {
            int temp = min;
            min = max;
            max = temp;
        }
        return random.nextInt(max - min) + min;
    }
}

Related Tutorials