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 
blog comments powered by Disqus

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
Domain Naming Service(DNS)
It is very difficult to remember a set of numbers (IP address) to connect to the Internet. The Domain Naming Service(DNS) is used to overcome this problem. It maps one particular IP address to a string of characters. For example, www.java2novice.com implies com is the domain name reserved for US commercial sites, java2novice is the name of the company and www is the name of the specific computer, which is java2novice's server.

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] or you can comment on the page.

Most Visited Pages

Other Interesting Sites

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