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
Different types of Access Modifiers
public: Any thing declared as public can be accessed from anywhere.

private: Any thing declared as private can't be seen outside of its class.

protected: Any thing declared as protected can be accessed by classes in the same package and subclasses in the other packages.

default modifier: Can be accessed only to classes in the same package.
Famous Quotations
It is easier to fight for one’s principles than to live up to them.
-- Alfred Adler

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.