Returns a random letter [ a-z A-Z ]. - Android java.util

Android examples for java.util:Random String

Description

Returns a random letter [ a-z A-Z ].

Demo Code


//package com.java2s;

import java.util.Random;

public class Main {
    /**/*  w ww .j a va  2s .  c  om*/
     * Returns a random letter [ a-z A-Z ].
     */
    public static char randomLetter() {
        if (Math.random() < 0.5) {
            return randomLetterLcase();
        } else {
            return randomLetterUcase();
        }
    }

    /**
     * Returns a random lower case letter [ a-z ].
     */
    public static char randomLetterLcase() {
        Random r = new Random();
        return (char) (r.nextInt(26) + 'a');
    }

    /**
     * Returns a random upper case letter [ A-Z ].
     */
    public static char randomLetterUcase() {
        Random r = new Random();
        return (char) (r.nextInt(26) + 'A');
    }
}

Related Tutorials