JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to create random string with random characters?


Description:

Write a program to generate random string of length 10 charactors. Every time you call the method, the program should generate random string.


Code:
package com.java2novice.random;

import java.util.Random;

public class MyStringRandomGen {

	private static final String CHAR_LIST = 
		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	private static final int RANDOM_STRING_LENGTH = 10;
	
	/**
	 * This method generates random string
	 * @return
	 */
	public String generateRandomString(){
        
		StringBuffer randStr = new StringBuffer();
        for(int i=0; i<RANDOM_STRING_LENGTH; i++){
        	int number = getRandomNumber();
        	char ch = CHAR_LIST.charAt(number);
        	randStr.append(ch);
        }
        return randStr.toString();
    }
	
	/**
     * This method generates random numbers
     * @return int
     */
    private int getRandomNumber() {
        int randomInt = 0;
        Random randomGenerator = new Random();
        randomInt = randomGenerator.nextInt(CHAR_LIST.length());
        if (randomInt - 1 == -1) {
            return randomInt;
        } else {
            return randomInt - 1;
        }
    }
    
    public static void main(String a[]){
    	MyStringRandomGen msr = new MyStringRandomGen();
    	System.out.println(msr.generateRandomString());
    	System.out.println(msr.generateRandomString());
    	System.out.println(msr.generateRandomString());
    	System.out.println(msr.generateRandomString());
    	System.out.println(msr.generateRandomString());
    	System.out.println(msr.generateRandomString());
    	System.out.println(msr.generateRandomString());
    }
}


Output:
UdX5PwalMI
qj62F7zuoR
ndJUNiHSlh
YohYpF5MfA
mwX9UhI7Ci
bCn5bsk2em
pzMXQDYaDx
<< Previous Program 

List of Random class sample programs:

  1. Basic random number generator.
  2. How to generate random numbers in the given range?
  3. How to generate same random sequence everytime?
  4. How to change Random class seed value?
  5. How to create random string with random characters?
Knowledge Centre
wait Vs sleep methods
sleep(): It is a static method on Thread class. It makes the current thread into the "Not Runnable" state for specified amount of time. During this time, the thread keeps the lock (monitors) it has acquired.

wait(): It is a method on Object class. It makes the current thread into the "Not Runnable" state. Wait is called on a object, not a thread. Before calling wait() method, the object should be synchronized, means the object should be inside synchronized block. The call to wait() releases the acquired lock.
Famous Quotations
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.