Skip to content

Instantly share code, notes, and snippets.

@programaths
Created July 6, 2016 21:06
Show Gist options
  • Save programaths/4aec1b2b95bfe6b8703304eae700824a to your computer and use it in GitHub Desktop.
Save programaths/4aec1b2b95bfe6b8703304eae700824a to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"math"
"time"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func randomInt() int32 {
return rand.Int31()
}
func sum(nums []int64) (sum int64) {
for _,v:=range nums{
sum+=int64(v)
}
return
}
func avg(nums []int64) float32 {
return float32(sum(nums))/float32(len(nums))
}
func std(nums []int64) float64 {
std:=float64(0)
mean:=avg(nums)
for _,v:=range nums{
std+=math.Pow(float64(v)-float64(mean),float64(2))
}
std=math.Sqrt(std/float64(len(nums)))
return std
}
func main() {
var s int64
var numbers100 []int64=make([]int64,10000)
for k:=0;k<10000;k++ {
s=0
for i := 0; i < 10000; i++ {
s += int64(randomInt())
}
numbers100[k]=s;
//fmt.Println(s)
}
fmt.Printf("%f",std(numbers100))
fmt.Println()
var numbers3 []int64=make([]int64,10000)
for k:=0;k<10000;k++ {
s=0
for i := 0; i < 3; i++ {
s += int64(randomInt())
}
numbers3[k]=s;
//fmt.Println(s)
}
fmt.Printf("%f",std(numbers3))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment