File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed
Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ sample
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "bufio"
5+ "fmt"
6+ "math/rand"
7+ "os"
8+ "strconv"
9+ "time"
10+ )
11+
12+ func main () {
13+
14+ if len (os .Args ) != 2 {
15+ fmt .Fprintf (os .Stderr , "invalid argument. use a fraction to sample between 0.0 (no sampling) and 1.0 (100% sampling)" )
16+ os .Exit (1 )
17+ }
18+
19+ target , err := strconv .ParseFloat (os .Args [1 ], 64 )
20+ if err != nil || target < 0.0 || target > 1.0 {
21+ fmt .Fprintf (os .Stderr , "Unable to convert %q to a float between 0.0 and 1.0" , os .Args [1 ])
22+ os .Exit (1 )
23+ }
24+
25+ rand .Seed (time .Now ().UnixNano ())
26+ scanner := bufio .NewScanner (os .Stdin )
27+ for scanner .Scan () {
28+ if target < rand .Float64 () {
29+ continue
30+ }
31+ fmt .Printf ("%q" , scanner .Text ()) // Println will add back the final '\n'
32+ }
33+ if err := scanner .Err (); err != nil {
34+ fmt .Fprintln (os .Stderr , "reading standard input:" , err )
35+ os .Exit (2 )
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments