{"id":19782362,"url":"https://github.com/infinitifall/weighted-random","last_synced_at":"2026-05-05T10:34:33.753Z","repository":{"id":165578992,"uuid":"567185042","full_name":"Infinitifall/weighted-random","owner":"Infinitifall","description":"Generate a random integer between two numbers [a, b] (both inclusive) that has higher probability of being closer to one than the other as defined by a weight w","archived":false,"fork":false,"pushed_at":"2024-11-03T06:36:18.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-11T02:32:38.104Z","etag":null,"topics":["c","java","javascipt","python","rng","weighted-random"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Infinitifall.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":"2022-11-17T08:54:43.000Z","updated_at":"2024-11-03T06:36:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"63ee0267-db0b-4ce3-9b93-63cfc37ba89a","html_url":"https://github.com/Infinitifall/weighted-random","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Infinitifall%2Fweighted-random","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Infinitifall%2Fweighted-random/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Infinitifall%2Fweighted-random/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Infinitifall%2Fweighted-random/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Infinitifall","download_url":"https://codeload.github.com/Infinitifall/weighted-random/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241113238,"owners_count":19911856,"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":["c","java","javascipt","python","rng","weighted-random"],"created_at":"2024-11-12T06:04:58.255Z","updated_at":"2026-05-05T10:34:33.682Z","avatar_url":"https://github.com/Infinitifall.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Weighted RNG\n\nGenerate a random integer between two integers $[a, b]$ (both inclusive) that has higher probability of being closer to one than the other as defined by a floating-point weight $w$.\n\n- $w = 0$ will produce integers uniformly distributed between $a, b$\n- $w \u003e 0$ will produce integers closer to $a$ on average\n- $w \u003c 0$ will produce integers closer to $b$ on average\n- Swapping $a$ and $b$ is equivalent to negating $w$\n\n\n## Language implementations\n\nRun the example wrapper which generates random numbers between $0$ and $100$ weighted towards $0$ with a weight of $2.5$\n\n- C\n  - `make`\n  - `./example 0 100 2.5`\n- Python\n  - `python example.py 0 100 2.5`\n- Java\n  - `java wrgen/example.java 0 100 2.5`\n- Clojure\n  - `clojure -M -i wrgen.clj -i example.clj`\n- Javascript\n  - Figure it out lol\n\n\n## How it works\n\nA random variable with a probability density function $p(x) = [\\frac{1-w}{2^{1-w} - 0.5^{1-w}}] x ^{-w} \\; \\forall x \\in [0.5, 2], x\\neq 1$ is used to generate a number in the range $(0.5, 2)$ which is scaled to integers in $[a, b]$ and returned. This is achieved by hard coding the inverse of its cumulative distribution function. Negative weights are taken as positive and the result is flipped in the end to give consistent behavior. The expected runtime is $O(1)$.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finfinitifall%2Fweighted-random","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finfinitifall%2Fweighted-random","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finfinitifall%2Fweighted-random/lists"}