Created
March 23, 2017 16:24
-
-
Save isurum/39fef315a5ebb7c9d5045c1c4b1b3ad1 to your computer and use it in GitHub Desktop.
A random integer between (int)'a' and (int)'z' is, (int)((int)'a' + Math.random() * ((int)'z' - (int)'a' + 1). every character has a unique Unicode between 0 and FFFF in hexadecimal (65535 in decimal). To generate a random character is to generate a random integer between 0 and 65535 using the following expression (note that since 0 <= Math.rand…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package learnjava.general; | |
/** | |
* Created by Isuru on 23/03/2017. | |
*/ | |
public class RandomCharacter { | |
/** Generates random character */ | |
public static char getRandomCharacter(char ch1, char ch2){ | |
return (char) (ch1 + Math.random() * (ch2 - ch1 + 1)); | |
} | |
/** Generate a random lowercase letter */ | |
public static char getRandomLowerCaseLetter(){ | |
return getRandomCharacter('a', 'z'); | |
} | |
/** Generate a random uppercase letter */ | |
public static char getRandomUpperCaseLetter(){ | |
return getRandomCharacter('A', 'Z'); | |
} | |
/** Generate a random digit character */ | |
public static char getRandomDigitalCharacter(){ | |
return getRandomCharacter('0', '9'); | |
} | |
public static char getRandomCharacter(){ | |
return getRandomCharacter('\u0000', '\uFFFF'); | |
} | |
public static void main(String args[]){ | |
final int NUMBER_OF_CHARS = 175; | |
final int CHARS_PER_LINE = 25; | |
// Print random characters between 'a' and 'z', 25 chars per line | |
for(int i = 0; i < NUMBER_OF_CHARS; i++){ | |
char ch = RandomCharacter.getRandomLowerCaseLetter(); | |
if((i + 1) % CHARS_PER_LINE == 0){ | |
System.out.println(ch); | |
}else{ | |
System.out.print(ch); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment