Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function makeLinearCongruentialGenerator(seed) {
- const m = Math.pow(2, 31) - 1;
- const a = 16807;
- const c = 0;
- let z = seed;
- return function lcg() {
- z = (a * z + c) % m;
- return z / m;
- }
- }
- export default function consistentHash(value, buckets) {
- const lcg = makeLinearCongruentialGenerator(value);
- let prev;
- let next = 0;
- do {
- prev = next;
- next = Math.floor((prev + 1) / lcg());
- } while (next < buckets);
- return prev;
- }
Add Comment
Please, Sign In to add comment