Skip to content

Lorem Ipsum Generator — Swift source

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

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

// lorem-ipsum — Swift port: canonical Lorem Ipsum generator (sentences, paragraphs, blocks).

enum Lorem {
    /// The canonical word pool — identical across every port.
    static let 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(separator: " ").map(String.init)

    /// Uniform integer in [0, max); Int.random is non-crypto, matching the TS
    /// source's Math.random().
    static func randBelow(_ max: Int) -> Int {
        max <= 0 ? 0 : Int.random(in: 0..<max)
    }

    /// One capitalized sentence of minWords...maxWords random words.
    static func sentence(minWords: Int = 8, maxWords: Int = 16) -> String {
        let span = max(1, maxWords - minWords + 1)
        let n = minWords + randBelow(span)
        let body = (0..<n).map { _ in words[randBelow(words.count)] }.joined(separator: " ")
        // Capitalize the first character only. prefix/dropFirst are total
        // functions, so a zero-word body degrades to "." like the TS source
        // (unlike first!, which would trap).
        return body.prefix(1).uppercased() + body.dropFirst() + "."
    }

    /// sentences default-length (8..16 word) sentences joined by spaces.
    static func paragraph(sentences: Int = 5) -> String {
        (0..<sentences).map { _ in sentence() }.joined(separator: " ")
    }

    static func text(paragraphs: Int = 3, sentencesPerPara: Int = 5) -> String {
        (0..<paragraphs).map { _ in paragraph(sentences: sentencesPerPara) }
            .joined(separator: "\n\n")
    }
}

print(Lorem.text())

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 →