Browse Source

allows user to specify max for rand

master
Stephen McQuay 4 years ago
parent
commit
f351a7d6c8
Signed by: sm
GPG Key ID: C383C74875475AC8
  1. 17
      main.go

17
main.go

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

Loading…
Cancel
Save