Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Random;

public class Main {
    private static final String RANDOM_CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static String getRandomString(int length) {
        if (length <= 0)
            return null;

        StringBuffer sb = new StringBuffer();
        Random r = new Random();
        int range = RANDOM_CHARS.length();
        for (int i = 0; i < length; i++) {
            sb.append(RANDOM_CHARS.charAt(r.nextInt(range)));
        }
        return sb.toString();
    }
}