Last active
October 27, 2015 12:32
-
-
Save suapapa/359f4afe185e0e2b1c56 to your computer and use it in GitHub Desktop.
random pick n person from input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
박초롱 | |
윤보미 | |
정은지 | |
손나은 | |
김남주 | |
오하영 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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