allows user to specify max for rand

This commit is contained in:
Stephen McQuay 2018-05-05 09:43:01 -07:00
parent 5ec66c2c1e
commit f351a7d6c8
Signed by: sm
GPG Key ID: C383C74875475AC8

17
main.go
View File

@ -2,7 +2,9 @@ package main
import ( import (
"container/heap" "container/heap"
"flag"
"fmt" "fmt"
"math"
"math/rand" "math/rand"
"os" "os"
"sort" "sort"
@ -12,8 +14,12 @@ import (
const usage = "smerge [count, count, ... count]" const usage = "smerge [count, count, ... count]"
var max = flag.Int("max", 1000, "maximum random number for each source")
func main() { func main() {
args := os.Args[1:] flag.Parse()
args := flag.Args()
if len(args) < 1 { if len(args) < 1 {
fmt.Fprintf(os.Stderr, "usage: %v\n", usage) fmt.Fprintf(os.Stderr, "usage: %v\n", usage)
os.Exit(1) os.Exit(1)
@ -26,20 +32,21 @@ func main() {
os.Exit(1) os.Exit(1)
} }
srcs = append(srcs, source(i)) srcs = append(srcs, source(i, *max))
} }
f := fmt.Sprintf("%%%dd\n", int(math.Log10(float64(*max))))
for i := range merge(srcs...) { for i := range merge(srcs...) {
fmt.Printf("%20d\n", i) fmt.Printf(f, i)
} }
} }
func source(c int) <-chan int { func source(c, max int) <-chan int {
out := make(chan int) out := make(chan int)
go func() { go func() {
vals := make([]int, c) vals := make([]int, c)
src := rand.New(rand.NewSource(time.Now().UnixNano())) src := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < c; i++ { for i := 0; i < c; i++ {
vals[i] = src.Int() vals[i] = src.Intn(max)
} }
sort.Ints(vals) sort.Ints(vals)