Skip to content

Commit

Permalink
chore(orama): updates random rank function for zip tree
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva committed Sep 3, 2023
1 parent 38cc867 commit 3dc3c33
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions packages/orama/src/trees/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@ interface Node<V = unknown> {
rank: number
}

function randomRank (maxAttempts = 32): number {
let heads = 0

for (let i = 0; i < maxAttempts; i++) {
if (Math.random() >= 0.5) break
heads += 1
}

return heads
function randomRank(): number {
const r = Math.random()
return Math.floor(Math.log(1 - r) / Math.log(1 - 0.5))
}

export function create<V> (key: number, value: V): Node<V> {
Expand Down

0 comments on commit 3dc3c33

Please sign in to comment.