Created
August 27, 2015 13:17
-
-
Save seiflotfy/479242f49d23bf19272b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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