Skip to content

Instantly share code, notes, and snippets.

@dpordomingo
Created October 5, 2016 07:02
Show Gist options
  • Save dpordomingo/17d3c7637286966c3b8e9ca639c0d356 to your computer and use it in GitHub Desktop.
Save dpordomingo/17d3c7637286966c3b8e9ca639c0d356 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
//"strconv"
//"strings"
)
func main() {
count:=int64(150)
freq:=.1
size:=4
naiveHoles := naiveHolesGenerator(count, freq)
summary(naiveHoles)
draw(naiveHoles)
holes := holeGenerator(count, freq, size)
summary(holes)
draw(holes)
}
func naiveHolesGenerator(length int64, freq float64) []bool {
var bools []bool
var newBool bool
for i := int64(0); i < length; i++ {
newBool = randomBoolean(freq)
bools = append(bools, newBool)
}
return bools
}
func holeGenerator(length int64, freq float64, maxSize int) []bool {
var holes []bool
var extraHolesLength int
//var extraHoles []string
for i := int64(0); i < length; i++ {
isHole := randomBoolean(freq)
if isHole {
extraHolesLength = rand.Intn(maxSize)
} else if extraHolesLength > 0 {
isHole = true
extraHolesLength--
}
//extraHoles = append(extraHoles, strconv.Itoa(extraHolesLength))
holes = append(holes, isHole)
}
//fmt.Println(strings.Join(extraHoles, ""))
return holes
}
func randomBoolean(trueFreq float64) bool {
randNum := rand.Float64()
return randNum < trueFreq
}
func summary(data []bool) {
count := map[bool]int64{true: 0, false: 0}
for _, v := range data {
count[v]++
}
fmt.Printf("%v", count)
fmt.Println("")
}
func draw(data []bool) {
for _, v := range data {
symb := rune('+')
if v {
symb = rune(' ')
}
fmt.Print(string(symb))
}
fmt.Println("")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment