get random string from ThreadLocalRandom - Java java.lang

Java examples for java.lang:String Random

Description

get random string from ThreadLocalRandom

Demo Code


//package com.java2s;
import java.util.concurrent.ThreadLocalRandom;

public class Main {
    public static void main(String[] argv) {
        int length = 42;
        System.out.println(random(length));
    }//w  w  w  .  j a va 2 s .  com

    public static String random(int length) {
        StringBuilder builder = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            builder.append((char) (ThreadLocalRandom.current().nextInt(33,
                    128)));
        }
        return builder.toString();
    }
}

Related Tutorials