Ejemplo n.º 1
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")
	}
}
Ejemplo n.º 2
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)))
}
Ejemplo n.º 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])
	}
}
Ejemplo n.º 4
0
// prints table of running times to call threesumfast.Count for arrays
// of size 250, 500, 1000, 2000, and so forth, along with rations of running
// times between successive array sizes
func main() {
	r := rand.New(0)
	prev := timeTrial(125, r)

	for n := 250; true; n += n {
		// Print time for problem size N
		time := timeTrial(n, r)
		fmt.Printf("%6d %7.1f %5.1f\n", n, time, time/prev)
		prev = time
	}
}
Ejemplo n.º 5
0
func timeTrial(n int) float64 {
	const MAX = 1000000
	a := make([]int, n)
	r := rand.New(0)

	for i := 0; i < n; i++ {
		a[i] = r.UniformIRange(-MAX, MAX)
	}

	timer := stopwatch.New()
	_ = threesum.CountD(a)
	return timer.ElapsedTime()
}
Ejemplo n.º 6
0
func timeRandomInput(alg string, n, t int) float64 {
	// Use alg to sort t random arrays of length n
	r := rand.New(0)
	total, a := 0.0, make([]float64, n)

	for i := 0; i < t; i++ {
		// Perform one experiment (generate and sort an array)
		for j := 0; j < n; j++ {
			a[i] = r.UniformF()
		}

		total += time(alg, a)
	}

	return total
}
Ejemplo n.º 7
0
// $ go run main.go 5 100.0 200.0
func main() {
	if len(os.Args) <= 1 {
		fmt.Printf("usage: %s 5 100.0 200.0\n", filepath.Base(os.Args[0]))
		return
	}

	n, _ := strconv.Atoi(strings.TrimSpace(os.Args[1]))
	lo, _ := strconv.ParseFloat(strings.TrimSpace(os.Args[2]), 64)
	hi, _ := strconv.ParseFloat(strings.TrimSpace(os.Args[3]), 64)
	r := rand.New(99)

	for i := 0; i < n; i++ {
		x := r.UniformFRange(lo, hi)
		fmt.Printf("%.2f\n", x)
	}
}
Ejemplo n.º 8
0
func main() {
	if len(os.Args) <= 1 {
		fmt.Printf("usage: %s [1000|2000|4000|8000].txt\n", filepath.Base(os.Args[0]))
		return
	}

	n, _ := strconv.Atoi(os.Args[1])
	a := make([]int, n)
	r := rand.New(0)

	for i := 0; i < n; i++ {
		a[i] = r.UniformIRange(-1000000, 1000000)
	}

	timer := stopwatch.New()
	cnt := threesum.Count(a)
	time := timer.ElapsedTime()
	fmt.Printf("%d triples. %f seconds\n", cnt, time)
}
Ejemplo n.º 9
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])
	}
}