{"id":19758068,"url":"https://github.com/paurkedal/ppx_deriving_random","last_synced_at":"2025-10-01T06:31:25.813Z","repository":{"id":29544060,"uuid":"33083113","full_name":"paurkedal/ppx_deriving_random","owner":"paurkedal","description":"Syntax extension to generate random instances of types","archived":false,"fork":false,"pushed_at":"2016-02-17T18:34:35.000Z","size":24,"stargazers_count":6,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-15T07:42:04.353Z","etag":null,"topics":["ocaml","ppx"],"latest_commit_sha":null,"homepage":null,"language":"OCaml","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/paurkedal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-29T18:03:20.000Z","updated_at":"2022-04-11T03:35:27.000Z","dependencies_parsed_at":"2022-09-03T16:22:06.840Z","dependency_job_id":null,"html_url":"https://github.com/paurkedal/ppx_deriving_random","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paurkedal%2Fppx_deriving_random","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paurkedal%2Fppx_deriving_random/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paurkedal%2Fppx_deriving_random/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paurkedal%2Fppx_deriving_random/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paurkedal","download_url":"https://codeload.github.com/paurkedal/ppx_deriving_random/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234835400,"owners_count":18894219,"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":["ocaml","ppx"],"created_at":"2024-11-12T03:22:48.217Z","updated_at":"2025-10-01T06:31:25.480Z","avatar_url":"https://github.com/paurkedal.png","language":"OCaml","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ``ppx_deriving_random``\n\nThis syntax extension can add a random generator for a custom type\ndefinition.  Adding ``[@@deriving random]`` to the type definition ``foo``\nwill emit ``val random_foo : random_state -\u003e foo``.  An additional argument\nis prepended to the argument list for each type variable in the style of\nother ``deriving`` extensions.  That is,\n\n```ocaml\ntype ('a1, ..., 'aN) foo = ... [@@deriving random]\n```\n\nwill provide\n\n```ocaml\nval random_foo : (random_state -\u003e 'a1) ... (random_state -\u003e 'aN) -\u003e\n                 random_state -\u003e ('a1, ..., 'aN) foo\n```\n\n## Installation and Usage\n\nIf you use ``opam`` and don't mind adding another repo, you can install\n``ppx_deriving_random`` with\n\n```ocaml\nopam repo add paurkedal https://github.com/paurkedal/opam-repo-paurkedal.git\nopam install ppx_deriving_random\n```\n\nFor manual installation from a fresh Git checkout, make sure you have OASIS\nand ``ppx_deriving``, then\n```shell\noasis setup -setup-update dynamic\nocaml setup.ml -configure --prefix your-prefix\nmake\nmake install\n```\nwhere ``your-prefix/lib`` is in your findlib path.  The syntax extension can\nnow be enabled with ``-package ppx_deriving_random.ppx`` or by putting\n``package(ppx_deriving_random.ppx)`` in your ``_tags``.  To build the\nexample:\n```shell\nocamlfind ocamlopt -linkpkg -package ppx_deriving_random.ppx -package ppx_deriving.show free_magma.ml\n```\n\n## Context\n\nThe following definitions are assumed to be in scope:\n\n```ocaml\ntype random_state\n(* State passed to the random generators. *)\n\nval random_case : int -\u003e random_state -\u003e int\n(* A function used to select a random constructor or row field. The first\n   argument is the number of cases. The result must be in the range from 0\n   to the number of cases minus one and should be uniformly distributed. *)\n\nval random_case_30b : random_state -\u003e int\n(* When fields are weighted (see below), this is used instead of\n   random_case. In the returned value, bit 29 and downwards to the desired\n   precision must be random and bit 30 upwards must be zero. *)\n```\n\nThese can be bound to ``Random.State.t``, ``Random.State.int``, and\n``Random.State.bits``, respectively, but this is not done by default since\nadvanced applications may need to pass additional information in\n``random_state`` or use different random number generators.\n\nReferences to other types will produce additional calls to correspondingly\nnamed generators. You should provide generators with suitable distributions\nfor you usage in scope of the type definition. If needed, the generator\nused for a particular type reference ``u`` can be overridden with\n``[@random expr]``, where ``expr : random_state -\u003e u``. This allows using\ndifferent distributions for different instances of the same type. Another\nway to tweak the distribution is to define type aliases with custom\nrandom generators.\n\n## Example\n\n```ocaml\ntype 'a free_magma =\n  | Fm_gen of 'a\n  | Fm_mul of 'a free_magma * 'a free_magma\n  [@@deriving random]\n```\n\nwill generate\n\n```ocaml\nval random_free_magma : (random_state -\u003e 'a) -\u003e random_state -\u003e 'a free_magma\n```\n\nHowever, there is a problem with the generated function.  By default all\nbranches of the variant is equally probable, leading to a possibly diverging\ncomputation.  This can be fixed by weighing the constructors:\n\n```ocaml\ntype 'a free_magma =\n  | Fm_gen of 'a [@weight 3]\n  | Fm_mul of 'a free_magma * 'a free_magma [@weight 2]\n  [@@deriving random]\n```\n\nWeights can be int or float constants and defaults to 1.  The probability is\nthe weight divided by the sum of weights for the given variant.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaurkedal%2Fppx_deriving_random","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaurkedal%2Fppx_deriving_random","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaurkedal%2Fppx_deriving_random/lists"}