this is a bad commit message
This commit is contained in:
commit
8d7b109462
6 files changed
+188
No files matched your search
@@ -0,0 +1,44 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "crypto/aes"
|
||||||
|
|
||||||
|
var seed = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}
|
||||||
|
var idx = 0
|
||||||
|
|
||||||
|
var block, _ = aes.NewCipher([]byte{1, 4, 2, 5, 1, 4, 2, 5, 1, 4, 5, 2, 1, 4, 5, 2})
|
||||||
|
|
||||||
|
func getBytesForInt() []byte {
|
||||||
|
if idx < len(seed) {
|
||||||
|
idx += 8
|
||||||
|
return seed[idx-8 : idx]
|
||||||
|
}
|
||||||
|
block.Encrypt(seed, seed)
|
||||||
|
idx = 8
|
||||||
|
return seed[0:8]
|
||||||
|
}
|
||||||
|
|
||||||
|
func aesInt63() int64 {
|
||||||
|
intAsBytes := getBytesForInt()
|
||||||
|
ret := int64(intAsBytes[0])
|
||||||
|
ret += 265 * int64(intAsBytes[1])
|
||||||
|
ret += 265 * 265 * int64(intAsBytes[2])
|
||||||
|
ret += 265 * 265 * 265 * int64(intAsBytes[3])
|
||||||
|
ret += 265 * 265 * 265 * 256 * int64(intAsBytes[4])
|
||||||
|
ret += 265 * 265 * 265 * 256 * 256 * int64(intAsBytes[5])
|
||||||
|
ret += 265 * 265 * 265 * 256 * 256 * 256 * int64(intAsBytes[6])
|
||||||
|
ret += 265 * 265 * 265 * 256 * 256 * 256 * 256 * int64(intAsBytes[7])
|
||||||
|
|
||||||
|
if ret < 0 {
|
||||||
|
return -ret
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func aesRandom() float64 {
|
||||||
|
f := float64(aesInt63()) / (1 << 63)
|
||||||
|
if f == 1 {
|
||||||
|
f = 0
|
||||||
|
}
|
||||||
|
return f
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"math/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
var normalBoxMullerAlreadyComputer bool = false
|
||||||
|
var normalBoxMullerAlreadyComputedValue float64 = 0
|
||||||
|
|
||||||
|
func NormalBoxMuller(r *rand.Rand) float64 {
|
||||||
|
if normalBoxMullerAlreadyComputer {
|
||||||
|
normalBoxMullerAlreadyComputer = false
|
||||||
|
return normalBoxMullerAlreadyComputedValue
|
||||||
|
}
|
||||||
|
radius := math.Sqrt(-2 * math.Log(r.Float64()))
|
||||||
|
theta := 2 * math.Pi * r.Float64()
|
||||||
|
|
||||||
|
normalBoxMullerAlreadyComputer = true
|
||||||
|
normalBoxMullerAlreadyComputedValue = radius * math.Cos(theta)
|
||||||
|
return radius * math.Cos(theta)
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"math/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
var normalMarsagliaAlreadyComputer bool = false
|
||||||
|
var normalMarsagliaAlreadyComputedValue float64 = 0
|
||||||
|
|
||||||
|
func NormalMarsaglia(r *rand.Rand) float64 {
|
||||||
|
if normalMarsagliaAlreadyComputer {
|
||||||
|
normalMarsagliaAlreadyComputer = false
|
||||||
|
return normalMarsagliaAlreadyComputedValue
|
||||||
|
}
|
||||||
|
x, y := getPointInUnitDisc(r)
|
||||||
|
radius := math.Sqrt(x*x + y*y)
|
||||||
|
|
||||||
|
normalMarsagliaAlreadyComputer = true
|
||||||
|
coeff := math.Sqrt(-4 * math.Log(radius) / radius)
|
||||||
|
normalMarsagliaAlreadyComputedValue = x * coeff
|
||||||
|
return y * coeff
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPointInUnitDisc(r *rand.Rand) (float64, float64) {
|
||||||
|
x := 2 * (r.Float64() - 0.5)
|
||||||
|
y := 2 * (r.Float64() - 0.5)
|
||||||
|
if x*x+y*y < 1 {
|
||||||
|
return x, y
|
||||||
|
}
|
||||||
|
return getPointInUnitDisc(r)
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"math/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExpFloat64(r *rand.Rand) float64 {
|
||||||
|
return -math.Log(r.Float64())
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/bszcz/mt19937_64"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BenchmarkMersenneTwister(b *testing.B) {
|
||||||
|
r := mt19937_64.New()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
r.Real1()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStdUniforme01(b *testing.B) {
|
||||||
|
r := rand.New(rand.NewSource(99))
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
r.Float64()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkAESRandom(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
aesRandom()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStdExpoVariate(b *testing.B) {
|
||||||
|
r := rand.New(rand.NewSource(99))
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
r.ExpFloat64()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkExpoVariate(b *testing.B) {
|
||||||
|
r := rand.New(rand.NewSource(99))
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
ExpFloat64(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNormalBoxMuller(b *testing.B) {
|
||||||
|
r := rand.New(rand.NewSource(99))
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
NormalBoxMuller(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStdNormal(b *testing.B) {
|
||||||
|
r := rand.New(rand.NewSource(99))
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
r.NormFloat64()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNormalMarsaglia(b *testing.B) {
|
||||||
|
r := rand.New(rand.NewSource(99))
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
NormalMarsaglia(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
# uqam simulation
|
||||||
|
|
||||||
|
Exploratory project to look into performance of some method to simulate
|
||||||
|
most common random variable in comparison with the ones provided by Go's
|
||||||
|
standard library.
|
||||||
|
|
||||||
|
As one can expect the conclusion is to use the functions provided by Go
|
||||||
|
and not to write your own.
|
||||||
|
|
||||||
|
### running the benchmarks
|
||||||
|
Just use Go's benchmarking capabilities with the bellow line of code.
|
||||||
|
|
||||||
|
go test -bench .
|
||||||
Reference in new issue
Block a user