{"id":19858606,"url":"https://github.com/ashenfad/cljx-sampling","last_synced_at":"2025-03-21T18:22:39.501Z","repository":{"id":25196789,"uuid":"28620431","full_name":"ashenfad/cljx-sampling","owner":"ashenfad","description":"Consistent sampling and random numbers for Clojure/ClojureScript","archived":false,"fork":false,"pushed_at":"2017-08-23T18:24:39.000Z","size":13,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-19T03:18:00.651Z","etag":null,"topics":["clojure","clojurescript","random-number-generator","sampling"],"latest_commit_sha":null,"homepage":"","language":"Clojure","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ashenfad.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}},"created_at":"2014-12-30T06:33:09.000Z","updated_at":"2019-08-25T13:49:23.000Z","dependencies_parsed_at":"2022-08-23T19:50:35.410Z","dependency_job_id":null,"html_url":"https://github.com/ashenfad/cljx-sampling","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/ashenfad%2Fcljx-sampling","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashenfad%2Fcljx-sampling/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashenfad%2Fcljx-sampling/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashenfad%2Fcljx-sampling/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ashenfad","download_url":"https://codeload.github.com/ashenfad/cljx-sampling/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244570961,"owners_count":20474156,"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":["clojure","clojurescript","random-number-generator","sampling"],"created_at":"2024-11-12T14:24:06.644Z","updated_at":"2025-03-21T18:22:39.470Z","avatar_url":"https://github.com/ashenfad.png","language":"Clojure","funding_links":[],"categories":["Clojure"],"sub_categories":[],"readme":"# cljx-sampling\n\nThis Clojure/ClojureScript library provides consistent sampling and\nrandom number generation regardless of the environment. That means\nusing the same intial seed we can produce the exact same results\n(sampled items or random numbers) whether in the JVM or in the\nbrowser.\n\n## Installation\n\n`cljx-sampling` is available as a Maven artifact from\n[Clojars](https://clojars.org/cljx-sampling).\n\nFor [Leiningen](https://github.com/technomancy/leiningen):\n\n[![Clojars Project](http://clojars.org/cljx-sampling/latest-version.svg)](http://clojars.org/cljx-sampling)\n\n## Overview\n\nThis library has two useful namespaces:\n  - [`cljx-sampling.random`](#random-number-generation) : Offers seedable random number generation.\n  - [`cljx.sampling.core`](#sampling) : In-memory sampling over collections (borrows from\n[bigml/sampling](https://github.com/bigmlcom/sampling#simple-sampling)).\n\nAs we review each, feel free to follow along in the REPL:\n```clojure\nuser\u003e (ns demo\n        (:require [cljx-sampling.random :as random]\n                  [cljx-sampling.core :refer [sample]]))\n```\n\n## Random number generation\n\n`cljx-sampling.random` provides seedable random number generation\nusing a [32-bit Xorshift](http://www.jstatsoft.org/v08/i14/paper).\nWhile this random number generator is surprisingly strong for its\nsimplicity, it is not cryptographically secure.\n\nTo use, simply `create` a random number generator and then call\n`next-boolean!`, `next-int!`, `next-double!`, or `next-gaussian!` as\nneeded.\n\n```clojure\ndemo\u003e (def rng (random/create))\ndemo\u003e (random/next-boolean! rng)\ntrue\ndemo\u003e (random/next-int! rng)\n3321045053\ndemo\u003e (random/next-int! rng 8)\n4\ndemo\u003e (random/next-double! rng)\n0.5436311555095017\ndemo\u003e (random/next-double! rng 10)\n8.794576390646398\ndemo\u003e (random/next-gaussian! rng)\n0.09573863197719758\n```\n\nWhen we `create` a generator, we can provide a seed to make the\nnumbers deterministic.\n\n```clojure\ndemo\u003e (def rng1 (random/create \"foobar\"))\ndemo\u003e (def rng2 (random/create \"foobar\"))\ndemo\u003e (repeatedly 10 #(random/next-int! rng1 100))\n(51 14 22 53 21 32 9 97 63 65)\ndemo\u003e (repeatedly 10 #(random/next-int! rng2 100))\n(51 14 22 53 21 32 9 97 63 65)\n```\n\n## Sampling\n\n`cljx-sampling.core` provides in-memory random sampling over\ncollections. While the original population is kept in memory, the\nresulting sample is a lazy sequence.\n\nBy default, sampling is done [without replacement](http://www.ma.utexas.edu/users/parker/sampling/repl.htm). This\nis equivalent to a lazy [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle).\n\n```clojure\ndemo\u003e (sample (range 5))\n(2 3 1 0 4)\n```\n\nSetting `:replace` as true will sample with replacement. Since there\nis no limit to the number of items that may be sampled with\nreplacement from a population, the result will be an infinite length\nlist. So make sure to `take` however many samples are needed.\n\n```clojure\ndemo\u003e (take 10 (sample (range 5) :replace true))\n(2 3 3 2 4 1 1 1 3 0)\n```\n\nEach call to `sample` will return a new sample order.\n\n```clojure\ndemo\u003e (sample (range 5))\n(0 2 3 1 4)\ndemo\u003e (sample (range 5))\n(3 1 4 2 0)\n```\n\nSetting the `:seed` parameter allows the sample order to be\ndeterministic.\n\n```clojure\ndemo\u003e (sample (range 5) :seed 7)\n(1 4 2 0 3)\ndemo\u003e (sample (range 5) :seed 7)\n(1 4 2 0 3)\ndemo\u003e (sample (range 5) :seed \"foobar\")\n(2 1 0 4 3)\ndemo\u003e (sample (range 5) :seed \"foobar\")\n(2 1 0 4 3)\n```\n\n### Weighted Sampling\n\nA sample may be weighted using the `:weigh` parameter. If the\nparameter is supplied with a function that takes an item and produces\na non-negative weight, then the resulting sample will be weighted\naccordingly.\n\n```clojure\ndemo\u003e (take 5 (sample [:heads :tails]\n                      :weigh {:heads 0.5 :tails 0.5}\n                      :replace true))\n(:tails :heads :heads :heads :tails)\n```\n\nThe weights need not sum to 1.\n\n```clojure\ndemo\u003e (-\u003e\u003e (sample [:heads :tails]\n                   :weigh {:heads 2 :tails 1}\n                   :replace true)\n           (take 100)\n           (frequencies))\n{:heads 66, :tails 34}\n```\n\n## License\n\nCopyright (C) 2014-2017 - Adam Ashenfelter\n\nDistributed under the Apache License, Version 2.0.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashenfad%2Fcljx-sampling","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fashenfad%2Fcljx-sampling","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashenfad%2Fcljx-sampling/lists"}