get Random Password By Length - Java java.util

Java examples for java.util:Random

Description

get Random Password By Length

Demo Code


//package com.java2s;

import java.util.Random;

public class Main {
    private static char[] seed2 = { '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9' };

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

        for (int i = 0; i < length; i++) {
            newRandom.append(seed2[rd.nextInt(seed2.length)]);
        }/*from   w w w  .  j a v  a 2s .c o m*/

        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