Skip to content

Mock Data Generator — Zig 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 Zig implementation — the same logic the interactive tool runs, in a shareable, citable form.

//! mock-data-generator — Zig 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; u32 wrapping ops (+% *%) reproduce
//! the JS `|0` / `>>>` / Math.imul stream, so a seed rebuilds the same rows.
const std = @import("std");

const first = [_][]const u8{ "Ava", "Liam", "Noah", "Emma", "Olivia", "Aiden", "Sophia", "Mason" };
const last = [_][]const u8{ "Smith", "Johnson", "Williams", "Brown", "Jones", "Garcia", "Miller", "Davis" };

/// One PRNG step; `a` is the carried state, so draws stay in schema order.
fn next(a: *u32) f64 {
    a.* = a.* +% 0x6D2B79F5;
    var t: u32 = (a.* ^ (a.* >> 15)) *% (1 | a.*); // Math.imul via u32 wrap
    t = (t +% ((t ^ (t >> 7)) *% (61 | t))) ^ t;
    return @as(f64, @floatFromInt(t ^ (t >> 14))) / 4294967296.0; // >>> 0, scaled
}

fn pick(names: []const []const u8, r: f64) []const u8 {
    return names[@intFromFloat(r * @as(f64, @floatFromInt(names.len)))];
}

pub fn main() !void {
    const alloc = std.heap.page_allocator;
    const stdout = std.io.getStdOut().writer();
    var a: u32 = 1234; // seed

    var i: usize = 0;
    while (i < 3) : (i += 1) { // count
        // email: two draws, like the TS reference
        const f = try std.ascii.allocLowerString(alloc, pick(&first, next(&a)));
        const l = try std.ascii.allocLowerString(alloc, pick(&last, next(&a)));
        defer alloc.free(f);
        defer alloc.free(l);
        const age = 18 + @as(i32, @intFromFloat(next(&a) * 48)); // integer 18..65
        const active = next(&a) < 0.5; // boolean
        try stdout.print("{{ id={d}, email={s}.{s}@example.com, age={d}, active={} }}\n", .{
            i, f, l, age, active,
        });
    }
}

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 →