Skip to content

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

/*
 * mock-data-generator — C 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; the same (seed, count) always
 * rebuilds the same rows. 32-bit wrap-around arithmetic stands in for the JS
 * `|0` / `>>>` / Math.imul semantics.
 */
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>

static const char *FIRST[] = {
    "Ava", "Liam", "Noah", "Emma", "Olivia", "Aiden", "Sophia", "Mason",
};
static const char *LAST[] = {
    "Smith", "Johnson", "Williams", "Brown", "Jones", "Garcia", "Miller", "Davis",
};

/* One PRNG step; `a` is the carried state, so draws stay in schema order. */
static double rng(uint32_t *a)
{
    *a += 0x6D2B79F5u;
    uint32_t t = (*a ^ (*a >> 15)) * (1u | *a);    /* Math.imul via u32 wrap */
    t = (t + (t ^ (t >> 7)) * (61u | t)) ^ t;
    return (double)(t ^ (t >> 14)) / 4294967296.0; /* >>> 0, scaled to [0,1) */
}

static const char *pick(const char *names[], size_t n, uint32_t *a)
{
    return names[(size_t)(rng(a) * n)];
}

static void lower_into(char *dst, const char *src)
{
    size_t i;
    for (i = 0; src[i] != '\0'; i++)
        dst[i] = (char)tolower((unsigned char)src[i]);
    dst[i] = '\0';
}

/* email = first.last@example.com — two draws, like the TS reference. */
static void make_email(char *dst, size_t cap, uint32_t *a)
{
    char f[32], l[32];
    lower_into(f, pick(FIRST, sizeof FIRST / sizeof FIRST[0], a));
    lower_into(l, pick(LAST, sizeof LAST / sizeof LAST[0], a));
    snprintf(dst, cap, "%s.%s@example.com", f, l);
}

int main(void)
{
    uint32_t a = 1234u; /* seed */

    for (int i = 0; i < 3; i++) { /* count */
        char email[64];
        make_email(email, sizeof email, &a);
        int age = 18 + (int)(rng(&a) * (65 - 18 + 1)); /* integer min..max */
        int active = rng(&a) < 0.5;                    /* boolean */
        printf("{\"id\": %d, \"email\": \"%s\", \"age\": %d, \"active\": %s}%s\n",
               i, email, age, active ? "true" : "false", i < 2 ? "," : "");
    }
    return 0;
}

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 →