Skip to content

Instantly share code, notes, and snippets.

@cnaude
Created June 28, 2021 18:34
Show Gist options
  • Save cnaude/d198a16bb6c60d9fe9baec70a064c69d to your computer and use it in GitHub Desktop.
Save cnaude/d198a16bb6c60d9fe9baec70a064c69d to your computer and use it in GitHub Desktop.
create random strings based on file input
package main
import (
"bufio"
"flag"
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
func main() {
var a []string
inputFile := flag.String("i", "input.txt", "input file")
count := flag.String("c", "12", "count")
flag.Parse()
c, err := strconv.Atoi(*count)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
file, err := os.Open(*inputFile)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
a = append(a, scanner.Text())
}
rand.Seed(time.Now().UnixNano())
for {
for j := 0; j <= c-1; j++ {
r := rand.Intn(len(a))
fmt.Printf("%s ", a[r])
}
fmt.Println("")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment