We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 29fa16b commit f15d78eCopy full SHA for f15d78e
tools/awk/sample.awk
@@ -0,0 +1,37 @@
1
+#!/usr/bin/env awk -f
2
+#
3
+# Samples a column of values.
4
5
+# Usage: sample prob seed
6
7
+# Input:
8
+# - a column of values
9
10
+# Arguments:
11
+# - prob: success probability
12
+# - seed: pseudorandom number generator seed
13
14
+# Output:
15
+# - sample values
16
17
+# Example:
18
+# $ sample 0.1 $RANDOM
19
+
20
+BEGIN {
21
+ if (ARGC == 1) {
22
+ prob = 0.5
23
+ } else if (ARGC == 2) {
24
+ prob = ARGV[1]
25
+ ARGV[1] = ""
26
+ } else {
27
28
+ srand(ARGV[2])
29
30
+ ARGV[2] = ""
31
+ }
32
+ # No filenames so force stdin:
33
+ ARGV[ARGC++] = "-"
34
+}
35
+rand() <= prob {
36
+ print $0
37
0 commit comments