get Random String from char array - Android java.util

Android examples for java.util:Random String

Description

get Random String from char array

Demo Code


//package com.java2s;

import java.util.Random;

public class Main {
    public static String getRandomString() {
        char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
        int size = 20;

        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < size; i++) {
            char c = chars[random.nextInt(chars.length)];
            sb.append(c);/*ww w. j  av a  2  s .c  o m*/
        }

        return sb.toString();
    }
}

Related Tutorials