package main import ( "fmt" "math" "math/rand" "time" ) var ( rnd = rand.New(rand.NewSource(time.Now().UnixNano())) ) func diffpriv(value, sensitivity, epsilon float64) float64 { noise := laplaceDist(0, sensitivity/epsilon) fmt.Println("noise: ", noise) return round(float64(value) + noise) } // Returns a random value from a laplace // distribution with parameters u and b. func laplaceDist(u, b float64) float64 { uniform := rnd.Float64() uniform -= 0.5 return u - b*sgn(uniform)*math.Log(1-2*math.Abs(uniform)) } // The signum function func sgn(x float64) float64 { if x < 0 { return -1 } if x > 0 { return 1 } return 0 } func round(n float64) float64 { if n < 0 { return math.Ceil(n - 0.5) } return math.Floor(n + 0.5) }