aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown3
-rwxr-xr-xbin/mktd3
-rwxr-xr-xbin/myb3
-rwxr-xr-xbin/rnda3
-rwxr-xr-xbin/rndi8
-rwxr-xr-xbin/rndl3
-rwxr-xr-xbin/rnds11
-rw-r--r--man/man1/rnda.17
-rw-r--r--man/man1/rndf.17
-rw-r--r--man/man1/rndi.119
-rw-r--r--man/man1/rndl.16
-rw-r--r--man/man1/rnds.121
12 files changed, 70 insertions, 24 deletions
diff --git a/README.markdown b/README.markdown
index afd005d0..95153945 100644
--- a/README.markdown
+++ b/README.markdown
@@ -303,8 +303,9 @@ Installed by the `install-bin` target:
* `rfct(1)` formats ASCII RFCs.
* `rfcr(1)` does both, displaying in a pager if appropriate, like a
`man(1)` reader for RFCs.
-* Four toy random-number scripts (not for sensitive/dead-serious use):
+* Five toy random-number scripts (not for sensitive/dead-serious use):
* `rndi(1)` gets a random integer within two bounds
+ * `rnds(1)` attempts to get an optional random seed for `rndi(1)`
* `rnda(1)` uses `rndi(1)` to choose a random argument
* `rndf(1)` uses `rnda(1)` to choose a random file from a directory
* `rndl(1)` uses `rndi(1)` to choose a random line from files
diff --git a/bin/mktd b/bin/mktd
index 657ec31f..2a8f0e56 100755
--- a/bin/mktd
+++ b/bin/mktd
@@ -1,5 +1,6 @@
#!/bin/sh
# Try to make a random temp directory
-dn=${TMPDIR:-/tmp}/${1:-mktd}.$$.$(rndi 1 2147483648)
+seed=$(rnds)
+dn=${TMPDIR:-/tmp}/${1:-mktd}.$$.$(rndi 1 2147483648 "$seed")
mkdir -m 700 -- "$dn" || exit 1
printf '%s\n' "$dn"
diff --git a/bin/myb b/bin/myb
index 11827fdd..03937a31 100755
--- a/bin/myb
+++ b/bin/myb
@@ -20,4 +20,5 @@ if [ "$((num >= 0 || den >= 1))" -ne 1 ] ; then
fi
# Perform the test; that's our exit value
-test "$(rndi 1 "$den")" -le "$num"
+seed=$(rnds)
+test "$(rndi 1 "$den" "$seed")" -le "$num"
diff --git a/bin/rnda b/bin/rnda
index 94cc48d1..99042bf6 100755
--- a/bin/rnda
+++ b/bin/rnda
@@ -4,6 +4,7 @@ if [ "$#" -eq 0 ] ; then
printf >&2 'rnda: No args given\n'
exit 1
fi
-argi=$(rndi 1 "$#") || exit
+seed=$(rnds)
+argi=$(rndi 1 "$#" "$seed") || exit
shift "$((argi-1))"
printf '%s\n' "$1"
diff --git a/bin/rndi b/bin/rndi
index 3b0ea005..f6cd33da 100755
--- a/bin/rndi
+++ b/bin/rndi
@@ -1,9 +1,9 @@
#!/usr/bin/awk -f
-# Get a low-quality random number between two integers. Note that depending on
-# implementation, this might return the same number if run in the same second.
-# It's only for trivial purposes.
+# Get a low-quality random number between two integers. Depending on the awk
+# implementation, if you don't provide a third argument (a seed), you might get
+# very predictable random numbers based on the current epoch second.
BEGIN {
- srand()
+ srand(ARGV[3])
print int(ARGV[1]+rand()*(ARGV[2]-ARGV[1]+1))
exit
}
diff --git a/bin/rndl b/bin/rndl
index 17e90470..978fc194 100755
--- a/bin/rndl
+++ b/bin/rndl
@@ -13,5 +13,6 @@ if [ "$#" -eq 0 ] ; then
cat >"$td"/stdin
fi
lc=$(sed -- '$=;d' "$@") || exit
-ri=$(rndi 1 "$lc") || exit
+seed=$(rnds)
+ri=$(rndi 1 "$lc" "$seed") || exit
sed -- "$ri"'!d' "$@"
diff --git a/bin/rnds b/bin/rnds
new file mode 100755
index 00000000..650f15bf
--- /dev/null
+++ b/bin/rnds
@@ -0,0 +1,11 @@
+#!/bin/sh
+# Try to get a low-quality random seed from a random device if possible
+[ "$#" -le 1 ] || exit 2
+for dev in /dev/arandom /dev/urandom /dev/random '' ; do
+ [ -e "$dev" ] && break
+done
+[ -n "$dev" ] || exit 1
+count=${1:-32}
+dd if="$dev" bs=1 count="$count" 2>/dev/null |
+cksum |
+cut -d' ' -f1
diff --git a/man/man1/rnda.1 b/man/man1/rnda.1
index 09833927..3c573662 100644
--- a/man/man1/rnda.1
+++ b/man/man1/rnda.1
@@ -8,9 +8,10 @@
arg1 arg2 arg3
.SH DESCRIPTION
.B rnda
-prints a random choice from the given arguments. It uses rndi(1), which is not
-a high-quality random number source.
+prints a random choice from the given arguments. It uses rndi(1), which is
+probably not a high-quality source, but should differ within seconds and
+between runs on most systems.
.SH SEE ALSO
-rndf(1), rndi(1), rndl(1), rndn(6)
+rndf(1), rndi(1), rndl(1), rnds(1), rndn(6)
.SH AUTHOR
Tom Ryder <tom@sanctum.geek.nz>
diff --git a/man/man1/rndf.1 b/man/man1/rndf.1
index a01a1125..f523c3ac 100644
--- a/man/man1/rndf.1
+++ b/man/man1/rndf.1
@@ -11,9 +11,10 @@
.SH DESCRIPTION
.B rndf
prints the name a random file (excluding dot files) from the given directory,
-defaulting to the current directory. It uses rndi(1), which is not a
-high-quality random number source.
+defaulting to the current directory. It uses rndi(1), which is probably not a
+high-quality source, but should differ within seconds and between runs on most
+systems.
.SH SEE ALSO
-rndi(1), rnda(1), rndl(1), rndn(6)
+rndi(1), rnda(1), rndl(1), rnds(1), rndn(6)
.SH AUTHOR
Tom Ryder <tom@sanctum.geek.nz>
diff --git a/man/man1/rndi.1 b/man/man1/rndi.1
index b645d504..094a5f62 100644
--- a/man/man1/rndi.1
+++ b/man/man1/rndi.1
@@ -1,20 +1,25 @@
.TH RNDI 1 "August 2016" "Manual page for rndi"
.SH NAME
.B rndi
-\- return a low-quality random integer between two bounds
+\- return a low-quality random integer
.SH SYNOPSIS
.B rndi
0 10
+.br
+.B rndi
+0 10 "$(rnds)"
.SH DESCRIPTION
.B rndi
returns a random integer ranging from the first argument to the second argument
-in a POSIX-compliant way (using awk).
+in a POSIX-compliant way (using awk), using the optional third argument as a
+seed.
.P
-The answer returned is low-quality; given some implementations of awk, it may
-even return the same result if run within the same second. This should not be
-used in any sort of security or statistical context. The author wrote it to
-support scripts to choose a random background image from a directory.
+The answer returned is low-quality; given some implementations of awk and no
+properly random seed, it may even return the same result if run within the same
+second. This should not be used in any sort of security or statistical context.
+The author wrote it to support scripts to choose a random background image from
+a directory.
.SH SEE ALSO
-rnda(1), rndf(1), rndl(1), rndn(6)
+rnda(1), rndf(1), rndl(1), rnds(1), rndn(6)
.SH AUTHOR
Tom Ryder <tom@sanctum.geek.nz>
diff --git a/man/man1/rndl.1 b/man/man1/rndl.1
index e9f697b3..69deb42b 100644
--- a/man/man1/rndl.1
+++ b/man/man1/rndl.1
@@ -13,8 +13,10 @@ command |
.B rndl
.SH DESCRIPTION
.B rndl
-prints a random line from its input, using rndi(1) to choose it.
+prints a random line from its input, using rndi(1) to choose it. It uses
+rndi(1), which is probably not a high-quality source, but should differ within
+seconds and between runs on most systems.
.SH SEE ALSO
-rndi(1), rnda(1), rndf(1), rndn(6)
+rndi(1), rnda(1), rndf(1), rnds(1), rndn(6)
.SH AUTHOR
Tom Ryder <tom@sanctum.geek.nz>
diff --git a/man/man1/rnds.1 b/man/man1/rnds.1
new file mode 100644
index 00000000..1e76d9a1
--- /dev/null
+++ b/man/man1/rnds.1
@@ -0,0 +1,21 @@
+.TH RNDS 1 "August 2016" "Manual page for rnds"
+.SH NAME
+.B rnds
+\- try to get a random seed
+.SH SYNOPSIS
+.B rnds
+.B rnds
+1024
+.SH DESCRIPTION
+.B rnds
+uses POSIX tools to try and find a random number device on the system and emits
+the first field of a cksum(1) based on it as a low-quality random numeric seed.
+The only optional argument allows specifying the number of random bytes to
+read, defaulting to 32. This is intended as a low-quality seed for rndi(1).
+.P
+/dev/arandom is tried first, then /dev/urandom, then /dev/random, before the
+script gives up and emits nothing.
+.SH SEE ALSO
+rndi(1), rnda(1), rndf(1), rndl(1), rndn(6)
+.SH AUTHOR
+Tom Ryder <tom@sanctum.geek.nz>