randomLevel returns a random level for the new skiplist node we are going to create. The return value of this function is between 1 and SkipListMaxLevel (both inclusive), with a powerlaw-alike distribution where higher levels are lesl likely to be returned.
()
| 320 | // (both inclusive), with a powerlaw-alike distribution where higher |
| 321 | // levels are lesl likely to be returned. |
| 322 | func randomLevel() int { |
| 323 | level := 1 |
| 324 | |
| 325 | for float64(rand.Int31()&0xFFFF) < SkipListP*0xFFFF { |
| 326 | level += 1 |
| 327 | } |
| 328 | if level < SkipListMaxLevel { |
| 329 | return level |
| 330 | } |
| 331 | |
| 332 | return SkipListMaxLevel |
| 333 | } |
| 334 | |
| 335 | func newSkipList(db *DB) *SkipList { |
| 336 | skipList := &SkipList{ |