Guest User

Untitled

a guest
Jul 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. function makeLinearCongruentialGenerator(seed) {
  2. const m = Math.pow(2, 31) - 1;
  3. const a = 16807;
  4. const c = 0;
  5.  
  6. let z = seed;
  7. return function lcg() {
  8. z = (a * z + c) % m;
  9. return z / m;
  10. }
  11. }
  12.  
  13. export default function consistentHash(value, buckets) {
  14. const lcg = makeLinearCongruentialGenerator(value);
  15.  
  16. let prev;
  17. let next = 0;
  18.  
  19. do {
  20. prev = next;
  21. next = Math.floor((prev + 1) / lcg());
  22. } while (next < buckets);
  23.  
  24. return prev;
  25. }
Add Comment
Please, Sign In to add comment