blob: 3abf730a56d427bd524cc43c8c83128e570df961 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#!/bin/sh
# Esoteric random number generator
# Single optional argument is a random seed, otherwise use rnds(1)
s=${1:-"$(rnds)"}
# Validate s
case $s in
*[!0-9]*)
printf >&2 'rndn: Seed must be non-negative integer\n'
exit 2
;;
esac
# Helper functions
t() {
printf %u "$1" | cut -c -"$2"
}
l() {
printf %u "$1" | wc -c
}
c() {
printf %u "$1" | cut -c "$2"
}
# Apply algorithm; you are not expected to understand this
s=$(t "$((s + 10))" 32) i=1 t=0
while [ "$i" -le "$(l "$s")" ] ; do
d=$(c "$s" "$i")
t=$((t + d)) i=$((i + 1))
done
p=$((s - t))
while [ "$(l "$p")" -gt 1 ] ; do
j=1 q=0
while [ "$j" -le "$(l "$p")" ] ; do
d=$(c "$p" "$j")
q=$((q + d)) j=$((j + 1))
done
p=$q
done
# Print result
printf '%u\n' "$p"
|