{"id":13711729,"url":"https://github.com/pblischak/zprob","last_synced_at":"2025-04-09T17:37:24.795Z","repository":{"id":177146553,"uuid":"635093789","full_name":"pblischak/zprob","owner":"pblischak","description":"A Zig Module for Random Number Distributions","archived":false,"fork":false,"pushed_at":"2025-04-04T21:47:29.000Z","size":2168,"stargazers_count":9,"open_issues_count":6,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T22:19:04.502Z","etag":null,"topics":["module","probability","random-sampling","statistics","zig"],"latest_commit_sha":null,"homepage":"https://pblischak.github.io/zprob/","language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pblischak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-02T00:31:38.000Z","updated_at":"2025-02-20T00:35:52.000Z","dependencies_parsed_at":"2024-04-26T19:24:29.904Z","dependency_job_id":"759d8dc7-4e10-431a-a3bc-6cd8eb183190","html_url":"https://github.com/pblischak/zprob","commit_stats":null,"previous_names":["pblischak/zprob"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pblischak%2Fzprob","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pblischak%2Fzprob/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pblischak%2Fzprob/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pblischak%2Fzprob/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pblischak","download_url":"https://codeload.github.com/pblischak/zprob/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248078387,"owners_count":21044098,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["module","probability","random-sampling","statistics","zig"],"created_at":"2024-08-02T23:01:11.058Z","updated_at":"2025-04-09T17:37:24.779Z","avatar_url":"https://github.com/pblischak.png","language":"Zig","funding_links":[],"categories":["Recently Updated","Data \u0026 Science"],"sub_categories":["[Who Wants to Be a Millionare](https://www.boardgamecapital.com/who-wants-to-be-a-millionaire-rules.htm)","Linear Algebra"],"readme":"\u003cdiv align=\"center\"\u003e\n\u003ch1\u003e\u003ctt\u003ezprob\u003c/tt\u003e\u003c/h1\u003e\n\u003ch3\u003e\u003ci\u003e\nA Zig Module for Random Number Distributions\n\u003c/i\u003e\u003c/h3\u003e\n\u003c/div\u003e\n\nThe `zprob` module implements functionality for working with probability distributions in pure Zig,\nincluding generating random samples and calculating probabilities using mass/density functions.\nThe instructions below will get you started with integrating `zprob` into your project, as well as\nintroducing some basic use cases. For more detailed information on the different APIs that `zprob`\nimplements, please refer to the [docs site](https://github.com/pblischak/zprob).\n\n## Getting Started\n\n### `RandomEnvironment` API\n\nBelow we show a small example program that introduces the `RandomEnvironment` struct, which\nprovides a high-level interface for sampling from distributions and calculating probabilities. It\nautomatically generates and stores everything needed to begin generating random numbers\n(seed + random generator), and follows the standard Zig convention of initialization with an\n`Allocator` that handles memory allocation.\n\n```zig\nconst std = @import(\"std\");\nconst zprob = @import(\"zprob\");\n\npub fn main() !void {\n    // Set up main memory allocator and defer deinitilization\n    var gpa = std.heap.DebugAllocator(.{}){};\n    const allocator = gpa.allocator();\n    defer {\n        const status = gpa.deinit();\n        std.testing.expect(status == .ok) catch {\n            @panic(\"Memory leak!\");\n        };\n    }\n\n    // Set up random environment and defer deinitialization\n    var env = try zprob.RandomEnvironment.init(allocator);\n    defer env.deinit();\n\n    // Generate random samples\n    const binomial_sample = try env.rBinomial(10, 0.8);\n    const geometric_sample = try env.rGeometric(3.0);\n\n\n    // Generate slices of random samples. The caller is responsible for cleaning up\n    // the allocated memory for the slice.\n    const binomial_slice = try env.rBinomialSlice(100, 20, 0.4);\n    defer allocator.free(binomial_samples);\n}\n```\n\nTo initialize a `RandomEnvironment` with a particular seed, use the `initWithSeed` method:\n\n```zig\nvar env = try zprob.RandomEnvironment.initWithSeed(1234567890, allocator);\ndefer env.deinit();\n```\n\n### Distributions API\n\nWhile the easiest way to get started using `zprob` is with the `RandomEnvironment` struct,\nfor users wanting more fine-grained control over the construction and usage of different probability\ndistributions, `zprob` provides a lower level \"Distributions API\".\n\n```zig\nconst std = @import(\"std\");\nconst zprob = @import(\"zprob\");\n\npub fn main() !void {\n    // Set up random generator.\n    const seed: u64 = @intCast(std.time.microTimestamp());\n    var prng = std.Random.DefaultPrng.init(seed);\n    var rand = prng.random();\n\n    var beta = zprob.Beta(f64).init(\u0026rand);\n    var binomial = zprob.Binomial(u8, f64).init(\u0026rand);\n\n    var b1: f64 = undefined;\n    var b2: u8 = undefined;\n    for 0..100 |_| {\n        b1 = try beta.sample(1.0, 5.0);\n        b2 = try binomial.sample(20, b1);\n    }\n}\n```\n\n## Example Projects\n\nAs mentioned briefly above, there are several projects in the\n[examples/](https://github.com/pblischak/zprob/tree/main/examples) folder that demonstrate the\nusage of `zprob` for different applications:\n\n- **approximate_bayes:** Uses approximate Bayesian computation to estimate the posterior mean\n  and standard deviation of a normal distribution using a small sample of observations.\n- **compound_distributions:** Illustrates how to generate samples from compound probability\n  distributions such as the Beta-Binomial.\n- **distribution_sampling:** Shows the basics of the \"Distributions API\" through the construction\n  of distribution structs with different underlying types.\n- **enemy_spawner:** Shows a gamedev motivated use case where distinct enemy types are sampled\n  with different frequencies, are given different stats based on their type, and are placed randomly\n  on the level map.\n\n## Available Distributions\n\n**Discrete Probability Distributions**\n\n[Bernoulli](https://en.wikipedia.org/wiki/Bernoulli_distribution) ::\n[Binomial](https://en.wikipedia.org/wiki/Binomial_distribution) ::\n[Geometric](https://en.wikipedia.org/wiki/Geometric_distribution) ::\n[Multinomial](https://en.wikipedia.org/wiki/Multinomial_distribution) ::\n[Negative Binomial](https://en.wikipedia.org/wiki/Negative_binomial_distribution) ::\n[Poisson](https://en.wikipedia.org/wiki/Poisson_distribution) ::\n[Uniform](https://en.wikipedia.org/wiki/Discrete_uniform_distribution)\n\n**Continuous Probability Distributions**\n\n[Beta](https://en.wikipedia.org/wiki/Beta_distribution) ::\n[Cauchy](https://en.wikipedia.org/wiki/Cauchy_distribution) ::\n[Chi-squared](https://en.wikipedia.org/wiki/Chi-squared_distribution) ::\n[Dirichlet](https://en.wikipedia.org/wiki/Dirichlet_distribution) ::\n[Exponential](https://en.wikipedia.org/wiki/Exponential_distribution) ::\n[Gamma](https://en.wikipedia.org/wiki/Gamma_distribution) ::\n[Normal](https://en.wikipedia.org/wiki/Normal_distribution) ::\n[Uniform](https://en.wikipedia.org/wiki/Continuous_uniform_distribution)\n\n## Installation\n\nTo include `zprob` in your Zig project, you can add it to your `build.zig.zon` file\nusing the `zig fetch` command.\n\nThe `main` branch tracks the latest release of Zig (currently v0.14.0) and can be added\nas follows:\n\n```\nzig fetch --save git+https://github.com/pblischak/zprob/\n```\n\nThe `nightly` branch tracks the Zig `master` branch and can be added by adding `#nightly` to the\ngit URL:\n\n```\nzig fetch --save git+https://github.com/pblischak/zprob/#nightly\n```\n\nThen, in the `build.zig` file, add the following lines within the `build` function to include\n`zprob` as a module:\n\n```zig\npub fn build(b: *std.Build) void {\n    // exe setup...\n\n    const zprob_dep = b.dependency(\"zprob\", .{\n            .target = target,\n            .optimize = optimize,\n    });\n\n    const zprob_module = zprob_dep.module(\"zprob\");\n    exe.root_module.addImport(\"zprob\", zprob_module);\n\n    // additional build steps...\n}\n```\n\nCheck out the build files in the [examples/](https://github.com/pblischak/zprob/tree/main/examples)\nfolder for some demos of complete sample code projects.\n\n## Issues\n\nIf you run into any problems while using `zprob`, please consider filing an issue describing the\nproblem, as well as any steps that may be required to reproduce the problem.\n\n## Contributing\n\nWe are open for contributions! Please see our contributing guide for more information on how you\ncan help build new features for `zprob`.\n\n## Other Useful Links\n\n- [https://ziglang.org/documentation/master/std/#std.Random](https://ziglang.org/documentation/master/std/#std.Random)\n- [https://zig.guide/standard-library/random-numbers](https://zig.guide/standard-library/random-numbers)\n- [https://github.com/statrs-dev/statrs](https://github.com/statrs-dev/statrs)\n- [https://github.com/rust-random/rand_distr](https://github.com/rust-random/rand_distr)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpblischak%2Fzprob","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpblischak%2Fzprob","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpblischak%2Fzprob/lists"}