Skip to content

Instantly share code, notes, and snippets.

@suapapa
Last active October 27, 2015 12:32
Show Gist options
  • Save suapapa/359f4afe185e0e2b1c56 to your computer and use it in GitHub Desktop.
Save suapapa/359f4afe185e0e2b1c56 to your computer and use it in GitHub Desktop.
random pick n person from input
박초롱
윤보미
정은지
손나은
김남주
오하영
// go run randpick.go -n3 < apink.txt
package main
import (
"bufio"
"flag"
"fmt"
"math/rand"
"os"
"time"
)
var flagN int
func init() {
rand.Seed(time.Now().UnixNano())
flag.IntVar(&flagN, "n", 1, "how many to pick")
flag.Parse()
}
func main() {
candidate := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
candidate = append(candidate, scanner.Text())
}
if err := scanner.Err(); err != nil {
panic(err)
}
fmt.Println("candidates are", candidate)
p := rand.Perm(len(candidate))
for i, j := range p {
fmt.Println("The winner is", candidate[j])
if i+1 >= flagN {
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment