-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrand_func.go
48 lines (36 loc) · 899 Bytes
/
rand_func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package goskiplist
import (
"math/rand"
)
const mask = ((1 << SkiplistMaxLevel) - 1)
//FAST used to initialise skiplist with
// fast random level generation algorithm but
// set probability of 0.5
const FAST = true
//VARIABLE used to initialise skiplist with
//slower random level generation algorithm but
//with variable probability
const VARIABLE = false
func coinTosses(prob float64, maxLevels int, fast bool) (counter int) {
counter = 1
// very fast with probability 0.5
// only one call to rand
// find first zero in random float bit representation
// geometric distribution
if fast {
resMask := rand.Uint64() & mask
// find first zero in float representation
for ; resMask&1 == 0; resMask >>= 1 {
counter++
}
return counter
}
// supports probability
// slower
res := rand.Float64()
for res < prob {
res = rand.Float64()
counter++
}
return counter
}