Skip to content

Instantly share code, notes, and snippets.

@seiflotfy
Created August 27, 2015 13:17
Show Gist options
  • Save seiflotfy/479242f49d23bf19272b to your computer and use it in GitHub Desktop.
Save seiflotfy/479242f49d23bf19272b to your computer and use it in GitHub Desktop.
package pmc
import (
"hash/fnv"
"math/rand"
"strconv"
)
func georand(w uint) uint8 {
hasher := fnv.New64a()
i := rand.Int()
hasher.Write([]byte(strconv.Itoa(i)))
val := hasher.Sum64()
// Calculate the position of the leftmost 1-bit.
r := uint(1)
for val&0x8000000000000000 == 0 && r <= w {
r++
val <<= 1
}
return uint8(r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment