get Random String - Android java.util

Android examples for java.util:Random String

Description

get Random String

Demo Code

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class Main{

 
    public static String getRandomStr(char startChr, char endChr) {

        int randomInt;
        String randomStr = null;/*from   w w  w  .j a  v a2s .  c om*/
        int startInt = Integer.valueOf(startChr);
        int endInt = Integer.valueOf(endChr);

        if (startInt > endInt) {
            throw new IllegalArgumentException("Start String: " + startChr
                    + " End String: " + endChr);
        }

        try {
            SecureRandom rnd = new SecureRandom();

            do {
                randomInt = rnd.nextInt(endInt + 1)
            } while (randomInt < startInt);
            randomStr = (char) randomInt + "";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return randomStr;
    }

}

Related Tutorials