blob: 8359af90b22b96256cb6b0ddd692170ad2b71dc0 (
plain) (
tree)
|
|
# 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
}
|