Generating Random Characters in Java !

This is a program to generate random characters using ‘Math.random( )’ !

public class generaterandom

{

        final static int maxCharacters = 200;

        final static int charPerLine = 30;

        public static void main( String [] args )

        {

                genRandomCharacters( ‘a’, ‘z’ );

                genRandomCharacters( ‘A’, ‘Z’ );

                genRandomCharacters( ‘0’, ‘9’ );

        }

        public static void genRandomCharacters( char ch1, char ch2 )

        {

                System.out.println();

                System.out.println();

                System.out.println();

                for( int i = 1; i <= maxCharacters; i++ )

                {

                        System.out.print( (char)(Math.random() * (ch2 – ch1 + 1) + ch1));

                        if( i % charPerLine == 0 )

                                System.out.println();

                }

        }

}

Output:

Subhashs-MacBook-Pro:CJ25 subhash$ javac generaterandom.java

Subhashs-MacBook-Pro:CJ25 subhash$ java generaterandom

beeviqirenpucocngxbgrcgtggslmn

xadrbtuqjnlopddvpcstdzhipamneo

dksqlicpifhnhehfvdjygnjvpwyaxf

ajzvqilbhfpqhoisbcabugnejvcbss

edxmahwiptcurdgbgyjzddaeutmjer

lndxpvtbluoflcscexoulotsvvqcwr

maeetwswxhjsrddymwqg

RRCIGFRGJFXJAFHYFPSMMOTFQSCYVJ

RZGVTTGNCRXSYHCOFZNDBWVBFWQXLN

IOUGSZUAOTLFZMAUZNTYAESOIUVPPO

HDPNEZCIDMDFLTNUDWGMMAKQVDWIBK

NKBVHKKHCNCDSLRXFLPWCFJWAKROZK

TVMKCVUXCKKKMPXVQIHFRNCGSGAMVK

CFYEIJIRYWBKGHWZRGMO

964797353179754528341159221170

854003631646257482114159712836

701885820640887251781676577211

098350993955258229081507158545

980956741809790693737131229201

977262132312267171653641973687

 

About the Author:

Subhash.K.U, Principal Mentor,

Subhash Programming Classes, Bangalore

View My Complete Profile !

I love to teach you ! Click here !

My name is Subhash.K.U. I work as Principal Mentor at Subhash Programming Classes helping fresh engineering graduates and professionals crack their programming interviews through C, C++, Data Structures, Java/J2EE and Linux Programming. 

No Comment

You can post first response comment.

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter a message.