Example #1
0
func main() {
	if len(os.Args) <= 1 {
		fmt.Printf("usage: %s 10\n", filepath.Base(os.Args[0]))
		return
	}

	t, _ := strconv.Atoi(os.Args[1])
	r := rand.New(0)
	heads := counter.New("heads")
	tails := counter.New("tails")

	for i := 0; i < t; i++ {
		if r.Bernoulli(0.5) {
			heads.Increment()
		} else {
			tails.Increment()
		}
	}

	fmt.Println(heads)
	fmt.Println(tails)

	d := heads.Tally() - tails.Tally()
	fmt.Println("delta:", math.Abs(float64(d)))
}
Example #2
0
func main() {
	if len(os.Args) <= 1 {
		fmt.Printf("usage: %s 1000000\n", filepath.Base(os.Args[0]))
		return
	}

	t, _ := strconv.Atoi(os.Args[1])
	r := rand.New(0)
	heads := counter.New("heads")
	tails := counter.New("tails")

	for i := 0; i < t; i++ {
		if r.Bernoulli(0.5) {
			heads.Increment()
		} else {
			tails.Increment()
		}
	}

	if heads.Tally() == tails.Tally() {
		fmt.Println("Tie")
	} else {
		fmt.Println(max(heads, tails), "wins")
	}
}
Example #3
0
// Reads two command-line integers n and t; creates n counters;
// increments t counters at random; and prints results
func main() {
	if len(os.Args) <= 1 {
		fmt.Printf("usage: %s 5 10\n", filepath.Base(os.Args[0]))
		return
	}

	n, _ := strconv.Atoi(os.Args[1])
	t, _ := strconv.Atoi(os.Args[2])
	r := rand.New(1000)

	// create n counters
	hits := make([]*counter.Counter, n)
	for i := 0; i < n; i++ {
		hits[i] = counter.New(fmt.Sprintf("counter%d", i))
	}

	// increment t counters at random
	for i := 0; i < t; i++ {
		hits[r.UniformI(n)].Increment()
	}

	// print results
	for i := 0; i < n; i++ {
		fmt.Println(hits[i])
	}
}
Example #4
0
func main() {
	if len(os.Args) <= 1 {
		fmt.Printf("usage: %s 1000000\n", filepath.Base(os.Args[0]))
		return
	}

	const SIDES int = 6
	r := rand.New(0)
	t, _ := strconv.Atoi(os.Args[1])
	rolls := make([]*counter.Counter, SIDES+1)

	for i := 1; i <= SIDES; i++ {
		rolls[i] = counter.New(fmt.Sprintf("%d's", i))
	}

	for i := 0; i < t; i++ {
		result := r.UniformIRange(1, SIDES+1)
		rolls[result].Increment()
	}

	for i := 1; i <= SIDES; i++ {
		fmt.Println(rolls[i])
	}
}