Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/infinitifall/weighted-random
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
https://github.com/infinitifall/weighted-random
c java javascipt python rng weighted-random
Last synced: 4 days ago
JSON representation
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
- Host: GitHub
- URL: https://github.com/infinitifall/weighted-random
- Owner: Infinitifall
- License: mit
- Created: 2022-11-17T08:54:43.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-03T06:36:18.000Z (13 days ago)
- Last Synced: 2024-11-03T07:21:44.614Z (13 days ago)
- Topics: c, java, javascipt, python, rng, weighted-random
- Language: Java
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Weighted RNG
Generate 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$.
- $w = 0$ will produce integers uniformly distributed between $a, b$
- $w > 0$ will produce integers closer to $a$ on average
- $w < 0$ will produce integers closer to $b$ on average
- Swapping $a$ and $b$ is equivalent to negating $w$## Language implementations
Run the example wrapper which generates random numbers between $0$ and $100$ weighted towards $0$ with a weight of $2.5$
- C
- `make`
- `./example 0 100 2.5`
- Python
- `python example.py 0 100 2.5`
- Java
- `java wrgen/example.java 0 100 2.5`
- Clojure
- `clojure -M -i wrgen.clj -i example.clj`
- Javascript
- Figure it out lol## How it works
A 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)$.