Create random String from characters in a default string - Android java.lang

Android examples for java.lang:Character

Description

Create random String from characters in a default string

Demo Code


//package com.java2s;

import java.util.Random;

public class Main {

    public static String randomStr(int length) {

        String hash = "";
        String charStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        char chars[] = charStr.toCharArray();
        int max = chars.length;
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            hash += chars[Math.abs(random.nextInt()) % max];
        }//from   w w w. j a  va2s .co  m

        return hash;
    }
}

Related Tutorials