aboutsummaryrefslogtreecommitdiff
path: root/bin/rndl.awk
diff options
context:
space:
mode:
Diffstat (limited to 'bin/rndl.awk')
-rw-r--r--bin/rndl.awk29
1 files changed, 29 insertions, 0 deletions
diff --git a/bin/rndl.awk b/bin/rndl.awk
new file mode 100644
index 00000000..8359af90
--- /dev/null
+++ b/bin/rndl.awk
@@ -0,0 +1,29 @@
+# Print a random line from input
+
+# Process arguments
+BEGIN {
+
+ # Name self
+ self = "rndl"
+
+ # Seed the random number generator
+ "rnds 2>/dev/null" | getline seed
+ srand(seed)
+}
+
+# Iterate over the lines, randomly assigning the first field of each one with a
+# decreasing probability
+rand() * NR < 1 { ln = $0 }
+
+# Check and print
+END {
+
+ # Check that we processed at least one line
+ if (!NR) {
+ printf "%s: No lines found on input\n", self | "cat >&2"
+ exit(1)
+ }
+
+ # Print the line
+ print ln
+}