Skip to content

Mock Data Generator — Swift source

Generate deterministic fake data (names, emails, numbers, dates, booleans, UUIDs, pick-from-list) from a schema and a seed. Reproducible output.

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

// mock-data-generator — Swift port of the CosmoDev tool.
// The seeded mulberry32 PRNG (bit-identical to src/lib/mockData.ts and the Go
// twin) drives a representative field set; UInt32 wraps with &+ / &* (Swift
// traps on plain overflow), reproducing the JS `|0` / `>>>` / Math.imul stream.
let first = ["Ava", "Liam", "Noah", "Emma", "Olivia", "Aiden", "Sophia", "Mason"]
let last = ["Smith", "Johnson", "Williams", "Brown", "Jones", "Garcia", "Miller", "Davis"]

/// Closure carries the state, mirroring the TS lib's draw order.
func mulberry32(seed: UInt32) -> () -> Double {
    var a = seed
    return {
        a = a &+ 0x6D2B79F5
        var t = (a ^ (a >> 15)) &* (1 | a)              // Math.imul
        t = (t &+ (t ^ (t >> 7)) &* (61 | t)) ^ t
        return Double(t ^ (t >> 14)) / 4294967296.0     // >>> 0, scaled to [0,1)
    }
}

/// Representative fields from the schema dispatch in mockData.ts.
func field(
    _ type: String, index: Int, _ rand: () -> Double,
    min: Int = 0, max: Int = 100
) -> Any? {
    switch type {
    case "index":
        return index                                    // no draw
    case "email":                                       // two draws, like TS
        let f = first[Int(rand() * Double(first.count))].lowercased()
        let l = last[Int(rand() * Double(last.count))].lowercased()
        return "\(f).\(l)@example.com"
    case "integer":
        return min + Int(rand() * Double(max - min + 1))
    case "boolean":
        return rand() < 0.5
    default:
        return nil                                      // unknown type -> nil
    }
}

let rand = mulberry32(seed: 1234)                       // seed
let schema: [(name: String, type: String, min: Int, max: Int)] = [
    ("id", "index", 0, 0),
    ("email", "email", 0, 0),
    ("age", "integer", 18, 65),
    ("active", "boolean", 0, 0),
]

for i in 0..<3 {                                        // count
    let row = schema.map { spec -> String in
        let value = field(spec.type, index: i, rand, min: spec.min, max: spec.max)
        return "\(spec.name)=\(value ?? "nil")"
    }
    print("[\(row.joined(separator: ", "))]")
}

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 →