Java Random Int randomText(int length)

Here you can find the source of randomText(int length)

Description

Create a random string with upper and lower case latin letters, spaces and paragraphs.

License

Open Source License

Parameter

Parameter Description
length a parameter

Declaration

public static String randomText(int length) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//w ww  . j  a  va2s .  co  m
     * Create a random string with upper and lower case latin letters,
     * spaces and paragraphs.
     * @param length
     * @return
     */
    public static String randomText(int length) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            sb.append(randomChar());
        }
        return sb.toString();
    }

    private static char randomChar() {
        int num = (int) Math.floor(Math.random() * 60);
        if (num < 26)
            return (char) ((int) 'a' + num);
        num -= 26;
        if (num < 26)
            return (char) ((int) 'A' + num);
        num -= 26;
        if (num < 1)
            return '\n';
        return ' ';
    }
}

Related

  1. randomString(int numChar)
  2. randomString(int size)
  3. randomString(int size, String... keys)
  4. randomStringNumbers(int len)
  5. randomtest(int max)
  6. randomWithinRange(int min, int max)
  7. randomWithRange(int min, int max)
  8. randomWord(int wordLength)
  9. randomZeroString(int length)