From e9d77b864c75d81ea7b1796fda0abe024756e8d1 Mon Sep 17 00:00:00 2001 From: Daniel Langesten Date: Tue, 24 Feb 2015 14:58:54 +0100 Subject: wrote function for generation of values in a laplace distribution --- diffpriv.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/diffpriv.go b/diffpriv.go index 22e844f..8b945d3 100644 --- a/diffpriv.go +++ b/diffpriv.go @@ -1,5 +1,24 @@ package main -func laplaceDist(u, b int) int { - return 4 //chosen by fair laplance weighted diceroll +import ( + "math/rand" +) + +// Returns a random value from a laplace +// distribution with parameters u and b. +func laplaceDist(u, b float64) float64 { + uniform := rand.Float64() + uniform -= 0.5 + return u - b*sgn(uniform)*math.Log(1-2*math.Abs(uniform)) +} + +// The signum function +func sgn(x float64) flot64 { + if x < 0 { + return -1 + } + if x > 0 { + return 1 + } + return 0 } -- cgit v1.1