Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 39 additions & 39 deletions p2p/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,58 @@ import (
"math/rand"
)

// LogNormalRand generates a log-normally distributed random number
// with given mu and sigma parameters.
func LogNormalRand(mu, sigma float64, src rand.Source) float64 {
r := rand.New(src)

u1 := r.Float64()
u2 := r.Float64()
z := math.Sqrt(-2.0*math.Log(u1)) * math.Cos(2*math.Pi*u2)

return math.Exp(mu + sigma*z)
}

// PoissonRand generates a Poisson-distributed random integer
// with given lambda parameter.
func PoissonRand(lambda float64, src rand.Source) int {
// Poisson(λ): discrete distribution.
func PoissonRandom(lambda float64) int {
L := math.Exp(-lambda)
k := 0
p := 1.0

r := rand.New(src)

for p > L {
k++
p *= r.Float64()
p *= rand.Float64()
}

return k - 1
}

// ExponentialRand generates an exponentially distributed random number
// with given rate parameter.
func ExponentialRand(rate float64, src rand.Source) float64 {
r := rand.New(src)
u := r.Float64()
return -math.Log(1-u) / rate
// LogNormal(μ, σ): continuous distribution.
func LogNormalRand(mu, sigma float64) float64 {
u1 := rand.Float64()
u2 := rand.Float64()
z := math.Sqrt(-2.0*math.Log(u1)) * math.Cos(2*math.Pi*u2)
return math.Exp(mu + sigma*z)
}

// UniformRand generates a uniformly distributed random number
// between min and max.
func UniformRand(min, max float64, src rand.Source) float64 {
r := rand.New(src)
return min + r.Float64()*(max-min)
// Exponential(λ): continuous distribution.
func ExponentialRandom(lambda float64) float64 {
u := rand.Float64()
return -math.Log(1.0-u) / lambda
}

// NormalRand generates a normally distributed random number
// with given mean and standard deviation.
func NormalRand(mean, stddev float64, src rand.Source) float64 {
r := rand.New(src)

u1 := r.Float64()
u2 := r.Float64()
// Normal(μ, σ): Box-Muller transform.
func NormalRandom(mu, sigma float64) float64 {
u1 := rand.Float64()
u2 := rand.Float64()
z := math.Sqrt(-2.0*math.Log(u1)) * math.Cos(2*math.Pi*u2)
return mu + sigma*z
}

// Binomial(n, p): discrete distribution.
func BinomialRandom(n int, p float64) int {
count := 0
for i := 0; i < n; i++ {
if rand.Float64() < p {
count++
}
}
return count
}

// Uniform(a, b): continuous distribution.
func UniformRandom(a, b float64) float64 {
return a + (b-a)*rand.Float64()
}

return mean + stddev*z
// Pareto(xm, α): continuous heavy-tailed distribution.
func ParetoRandom(xm, alpha float64) float64 {
u := rand.Float64()
return xm / math.Pow(1.0-u, 1.0/alpha)
}
11 changes: 4 additions & 7 deletions p2p/p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"math"
"math/rand"
"os"
"testing"
"time"
Expand All @@ -18,10 +17,9 @@ func TestGenerateNetwork(t *testing.T) {
sg := standard_graph.NewStandardGraph()
g := sg.ErdosRenyiGraph(1000, 50.000/1000, true)
t.Logf("Generated graph with %d nodes and %d edges\n", len(g.Nodes()), g.EdgeCount())
src := rand.NewSource(time.Now().UnixNano())

nodeLatency := func() float64 { return p2p.LogNormalRand(math.Log(100), 0.5, src) }
edgeLatency := func() float64 { return p2p.LogNormalRand(math.Log(100), 0.3, src) }
nodeLatency := func() float64 { return p2p.LogNormalRand(math.Log(100), 0.5) }
edgeLatency := func() float64 { return p2p.LogNormalRand(math.Log(100), 0.3) }

nw, err := p2p.GenerateNetwork(g, nodeLatency, edgeLatency, &p2p.Config{GossipFactor: 0.35})
if err != nil {
Expand Down Expand Up @@ -88,10 +86,9 @@ func TestMetrics(t *testing.T) {
sg := standard_graph.NewStandardGraph()
g := sg.ErdosRenyiGraph(1000, 50.000/1000, true)
t.Logf("Generated graph with %d nodes and %d edges\n", len(g.Nodes()), g.EdgeCount())
src := rand.NewSource(time.Now().UnixNano())

nodeLatency := func() float64 { return p2p.LogNormalRand(math.Log(100), 0.5, src) }
edgeLatency := func() float64 { return p2p.LogNormalRand(math.Log(100), 0.3, src) }
nodeLatency := func() float64 { return p2p.LogNormalRand(math.Log(100), 0.5) }
edgeLatency := func() float64 { return p2p.LogNormalRand(math.Log(100), 0.3) }

nw, err := p2p.GenerateNetwork(g, nodeLatency, edgeLatency, &p2p.Config{GossipFactor: 0.35})
if err != nil {
Expand Down