Skip to content

Lorem Ipsum Generator — Kotlin source

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

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

// lorem-ipsum — Kotlin port: canonical Lorem Ipsum generator (sentences, paragraphs, blocks).
import kotlin.random.Random

// The canonical word pool — identical across every port.
val 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(' ')

// Uniform integer in [0, max); 0 for a non-positive bound. kotlin.random is
// non-crypto, matching the TS source's Math.random().
fun randBelow(max: Int): Int = if (max <= 0) 0 else Random.nextInt(max)

// One capitalized sentence of minWords..maxWords random words.
fun sentence(minWords: Int = 8, maxWords: Int = 16): String {
    val span = maxOf(1, maxWords - minWords + 1)
    val n = minWords + randBelow(span)
    val body = List(n) { WORDS[randBelow(WORDS.size)] }.joinToString(" ")
    // Capitalize the first character only — replaceFirstChar, unlike
    // String#capitalize, does not lowercase the tail. An empty body yields "."
    // (replaceFirstChar on "" returns ""), matching the TS source.
    return body.replaceFirstChar { it.uppercase() } + "."
}

// sentences default-length (8..16 word) sentences joined by spaces.
fun paragraph(sentences: Int = 5): String = List(sentences) { sentence() }.joinToString(" ")

fun lorem(paragraphs: Int = 3, sentencesPerPara: Int = 5): String =
    List(paragraphs) { paragraph(sentencesPerPara) }.joinToString("\n\n")

fun main() = println(lorem())

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 →