Skip to content

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

# mock-data-generator — Ruby 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. Ruby Ints are unbounded, so every
# step masks back to 32 bits to reproduce the JS `|0` / `>>>` / Math.imul
# stream exactly.
FIRST = %w[Ava Liam Noah Emma Olivia Aiden Sophia Mason].freeze
LAST = %w[Smith Johnson Williams Brown Jones Garcia Miller Davis].freeze
U32 = 0xFFFFFFFF

# The lambda carries the state, mirroring the TS lib's draw order.
def mulberry32(seed)
  a = seed & U32
  lambda do
    a = (a + 0x6D2B79F5) & U32
    t = ((a ^ (a >> 15)) * (1 | a)) & U32                  # Math.imul
    t = ((t + (((t ^ (t >> 7)) * (61 | t)) & U32)) & U32) ^ t
    ((t ^ (t >> 14)) & U32) / 4294967296.0                 # >>> 0, scaled to [0,1)
  end
end

# Representative fields from the schema dispatch in mockData.ts.
def field(type, index, rand, min: 0, max: 100)
  case type
  when "index"   then index                                # no draw
  when "email"                                          # two draws, like TS
    f = FIRST[(rand.call * FIRST.size).to_i].downcase
    l = LAST[(rand.call * LAST.size).to_i].downcase
    "#{f}.#{l}@example.com"
  when "integer" then min + (rand.call * (max - min + 1)).to_i
  when "boolean" then rand.call < 0.5
  else nil                                                 # unknown type -> nil
  end
end

rand = mulberry32(1234)                                    # seed
schema = [
  { "name" => "id", "type" => "index" },
  { "name" => "email", "type" => "email" },
  { "name" => "age", "type" => "integer", "min" => 18, "max" => 65 },
  { "name" => "active", "type" => "boolean" },
]

3.times do |i|                                             # count
  row = schema.to_h do |spec|
    [spec["name"],
     field(spec["type"], i, rand,
           min: spec.fetch("min", 0), max: spec.fetch("max", 100))]
  end
  p row
end

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 →