Skip to content

Lorem Ipsum Generator — Java source

Generate placeholder Lorem Ipsum text - paragraphs and sentences with adjustable counts. Classic Latin word pool, instant copy.

This is the Java implementation — the same logic the interactive tool runs, in a shareable, citable form.

// lorem-ipsum — Java port: canonical Lorem Ipsum generator (sentences, paragraphs, blocks).
import java.util.Random;

public class LoremIpsum {
    // The canonical word pool — identical across every port.
    static final String[] WORDS =
        ("lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor " +
         "incididunt ut labore et dolore magna aliqua enim ad minim veniam quis nostrud exercitation " +
         "ullamco laboris nisi aliquip ex ea commodo consequat duis aute irure in reprehenderit " +
         "voluptate velit esse cillum eu fugiat nulla pariatur excepteur sint occaecat cupidatat " +
         "non proident sunt culpa qui officia deserunt mollit anim id est laborum").split(" ");

    static final Random RNG = new Random();  // non-crypto, matching the TS source

    // Uniform integer in [0, max); 0 for a non-positive bound.
    static int randBelow(int max) {
        return max <= 0 ? 0 : RNG.nextInt(max);
    }

    // One capitalized sentence of minWords..maxWords random words.
    static String sentence(int minWords, int maxWords) {
        int span = Math.max(1, maxWords - minWords + 1);
        int n = minWords + randBelow(span);
        StringBuilder body = new StringBuilder();
        for (int i = 0; i < n; i++) {
            if (i > 0) body.append(' ');
            body.append(WORDS[randBelow(WORDS.length)]);
        }
        // Capitalize the first character only — Character.toUpperCase leaves the
        // tail alone, unlike String#capitalize-style whole-word methods.
        if (body.length() > 0) {
            body.setCharAt(0, Character.toUpperCase(body.charAt(0)));
        }
        return body.append('.').toString();
    }

    static String sentence() {
        return sentence(8, 16);
    }

    // sentences default-length (8..16 word) sentences joined by spaces.
    static String paragraph(int sentences) {
        StringBuilder out = new StringBuilder();
        for (int i = 0; i < sentences; i++) {
            if (i > 0) out.append(' ');
            out.append(sentence());
        }
        return out.toString();
    }

    static String lorem(int paragraphs, int sentencesPerPara) {
        StringBuilder out = new StringBuilder();
        for (int i = 0; i < paragraphs; i++) {
            if (i > 0) out.append("\n\n");
            out.append(paragraph(sentencesPerPara));
        }
        return out.toString();
    }

    public static void main(String[] args) {
        System.out.println(lorem(3, 5));
    }
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →