Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java how do you randomly select a letter (a-z)?

Tags:

java

random

If I want to randomly select a letter between a and z, I assume I have to use the Random class:

Random rand = new Random();

But since this only generates numbers, what do I need to do to apply this to letters?

like image 801
Kristof von Huetenberg Avatar asked Mar 05 '11 09:03

Kristof von Huetenberg


People also ask

How do you randomly select an object in Java?

Here's how to pick a random one: int randomIndex = new Random(). nextInt(array. length); ObjectReturningSomething randomObject = objectArray.

How do you identify a letter in Java?

In order to check if a String has only Unicode letters in Java, we use the isDigit() and charAt() methods with decision-making statements. The isLetter(int codePoint) method determines whether the specific character (Unicode codePoint) is a letter. It returns a boolean value, either true or false.

How do you generate a string of random letters in Java?

Using randomUUID() util. UUID is another Java class that can be used to generate a random string. It offers a static randomUUID() method that returns a random alphanumeric string of 32 characters.

How do you check if a character is from A to Z in Java?

We can check whether the given character in a string is a number/letter by using isDigit() method of Character class. The isDigit() method is a static method and determines if the specified character is a digit.


6 Answers

Random r = new Random();
char c = (char) (r.nextInt(26) + 'a');
like image 57
Michael Barker Avatar answered Oct 11 '22 21:10

Michael Barker


Letters, or more exactly, characters, are numbers (from 0 to 255 in extended ascii, 0 to 127 in non-extended). For instance, in ASCII, 'A' (quote means character, as opposed to string) is 65. So 1 + 'A' would give you 66 - 'B'. So, you can take a random number from 0 to 26, add it to the character 'a', and here you are : random letter.

You could also do it with a string, typing "abcdefghijklmnopqrstuvwxyz" and taking a random position in this chain, but Barker solution is more elegant.

like image 23
Raveline Avatar answered Oct 11 '22 21:10

Raveline


alter version of @Michael Barker

Random r = new Random();
int c = r.nextInt(26) + (byte)'a';
System.out.println((char)c);
like image 25
Nishant Avatar answered Oct 11 '22 21:10

Nishant


char randomLetter = (char) ('a' + Math.random() * ('z'-'a' + 1));
like image 30
Joon Lee Avatar answered Oct 11 '22 21:10

Joon Lee


why assume to use Random instead of Math.random? You can even make the code shorter...

public static char genChar(){
    return (char)(Math.random()*26 + 'a');
}
like image 23
Miguel Angel Lozano Gomez Avatar answered Oct 11 '22 20:10

Miguel Angel Lozano Gomez


import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
...
randomAlphabetic(1).toLowerCase()

this gives you a string with single character

like image 41
piotrek Avatar answered Oct 11 '22 20:10

piotrek