Advertisement
zergon321

normal_to_uniform

Nov 13th, 2021
1,368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.49 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "math"
  6.     "math/rand"
  7. )
  8.  
  9. // uniform returns a uniformly random float in [0,1).
  10. func uniform() float64 {
  11.     i := rand.Uint64() % (1 << 52)
  12.  
  13.     return (1 + float64(i)/(1<<52)) / math.Pow(2, geometric())
  14. }
  15.  
  16. // geometric returns a number picked from a geometric
  17. // distribution of parameter 0.5.
  18. func geometric() float64 {
  19.     b := 1
  20.  
  21.     for rand.Uint64()%2 == 0 {
  22.         b++
  23.     }
  24.  
  25.     return float64(b)
  26. }
  27.  
  28. func main() {
  29.     rand.Seed(16)
  30.  
  31.     fmt.Println(uniform())
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement