-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrand
More file actions
527 lines (465 loc) · 17.8 KB
/
rand
File metadata and controls
527 lines (465 loc) · 17.8 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/bin/bash
# shellcheck disable=SC2039
# Purpose: This script attempts to generate random integers through any
# means necessary. Not to be used for any cryptography, it
# just generates random integers, that's all!
# Author: Rawiri Blundell
# Copyright: (c) 2016 - Beerware As-Is.
# No warranty or liability, but some attribution would be nice if
# it works well for you or you derive your own code.
# Just as I've attributed inspiration below.
# Date: 20160222
# Requires: At the very least, an interpreter with bitshifting.
# Interpreter: This isn't strict POSIX. This may have a bash shebang, but
# should work fine in any POSIX compatible shell
# #!/usr/bin/env ksh or #!/bin/ksh will likely work everywhere
###############################################################################
# Inspiration taken from:
# Colin Riddel provided the perl one liner that started this
# 'rand' by Heiner Steven:
# http://www.shelldorado.com/scripts/cmds/rand
# 'rand' by Malte Skoruppa:
# https://unix.stackexchange.com/q/157250
# 'randbits' by Gene Spafford:
# http://www.diablotin.com/librairie/networking/puis/ch23_09.htm
###############################################################################
# Set the default variable states
nMin=1
debug=false
nCount=1
repeat=false
zeroPad=false
tmpDir=/tmp/rand
# Simple 'die' function
die() {
case "${1}" in
(-e|--error) shift && printf '[ERROR] rand: %s\n' "${@}" 1>&2 ;;
(-f|--fatal) shift && printf '[FATAL] rand: %s\n' "${@}" 1>&2 ;;
(-i|--info) shift && printf '[INFO] rand: %s\n' "${@}" 1>&2 ;;
(*) printf '[INFO] rand: %s\n' "${@}" 1>&2 ;;
esac
exit 1
}
# Shorten 'printf' slightly
printint() {
case "${1}" in
(-u) shift && printf -- '%u\n' "${*}" ;;
(*) printf -- '%d\n' "${*}" ;;
esac
}
printstr() {
printf -- '%s\n' "${*}"
}
# Function to print out a help message
# Excluded from indentation rules as it's a heredoc
usage() {
cat << EOF
rand - generate random positive integers
Optional Arguments:
-d [debug. Tells you which processing method is used (Default:off)]
-h [help]
-m [minimum number (Default:${nMin})]
-M [maximum number (Default:${maxRand})]
-N [count. Number of numbers (Default:${nCount})]
-r [repeat. Output lines may be repeated (Default:off)]
-z [zero padding. E.g. With '-z 2', '9' becomes '09' (Default:off)]
EOF
}
# In case we actually use $tmpDir, let's trap and delete it
trap 'rm -rf "${tmpDir}"' EXIT INT TERM HUP
# Figure out our default maxRand, using 'getconf'
if [ "$(getconf LONG_BIT)" -eq 64 ]; then
# 2^63-1
maxRand=9223372036854775807
elif [ "$(getconf LONG_BIT)" -eq 32 ]; then
# 2^31-1
maxRand=2147483647
else
# 2^15-1
maxRand=32767
fi
# Override for mksh, which uses 32-bit arithmetic
# This test has to be separate to keep dash happy
case "${KSH_VERSION}" in
(*"MIRBSD"*) maxRand=2147483647 ;;
esac
# Getopts
while getopts ":dDhm:M:N:rz:" Flags; do
case "${Flags}" in
(d) debug="true" ;;
(D) debug="true"; set -x ;;
(h) usage && exit 0;;
(m) case "${OPTARG}" in
(*[!0-9]*|'') die -e "(-m) '${OPTARG}' is not a number" ;;
(*) nMin="${OPTARG}" ;;
esac;;
(M) case "${OPTARG}" in
(*[!0-9]*|'') die -e "(-M) '${OPTARG}' is not a number" ;;
(*) nMax="${OPTARG}" ;;
esac;;
(N) case "${OPTARG}" in
(*[!0-9]*|'') die -e "(-N) '${OPTARG}' is not a number" ;;
(*) nCount="${OPTARG}" ;;
esac;;
(r) repeat=true;;
(z) case "${OPTARG}" in
(*[!0-9]*|'') die -e "(-z) '${OPTARG}' is not a number" ;;
(*) zeroPad="${OPTARG}" ;;
esac;;
(\?) die -e "Invalid option: '-$OPTARG'. Try 'rand -h' for usage." ;;
(:) die -e "Option '-$OPTARG' requires an argument." ;;
esac
done
# In case nMax is blank, default to maxRand
[ -z "${nMax}" ] && nMax="${maxRand}"
# Easter Egg
if printint "${nMax}" 2>&1 | grep -E "out of range|too large" >/dev/null 2>&1; then
die -i "Come on now, stop being silly." \
"My upper boundary is *sometimes* '${maxRand}'."
fi
# Double check that we haven't done something silly like have nMax less than nMin
if [ "${nMin}" -ge "${nMax}" ]; then
die -e "(-m) minimum value is greater than or equal to (-M) maximum value"
fi
# If repeat is not set, then nCount cannot be higher than nMax
if [ "${repeat}" = "false" ] && [ "${nCount}" -gt "${nMax}" ]; then
printstr \
"[INFO] rand: Count (${nCount}) cannot be higher than the maximum boundary (${nMax})." \
"Count will be: ${nMax}. Consider the '-r' option." 1>&2
nCount="${nMax}"
fi
# Put these vars into the environment so we can import them to perl etc
export nMin nMax nCount
################################################################################
# Functions
################################################################################
# Check if a command exists
get_command() {
command -v "${*}" >/dev/null 2>&1
}
# Ensure that shuf is not a step-in function
# We've had an instance of a step-in function that depended on this script
# Chicken, meet egg. Bok bok cluck cluck.
get_shuf_type() {
if type shuf | head -n 1 | grep function > /dev/null 2>&1; then
return 1
fi
return 0
}
# This function simply outputs the method used for random integer generation
print_debug() {
[ "${debug}" = true ] && printstr "[DEBUG] rand: Method used is '$*'" 1>&2
}
# Function to generate a reliable seed for whatever method requires one
# Because 'date +%s' isn't entirely portable, we also try other methods
get_seed() {
# First we check if /dev/urandom is available.
# We used to have a get_int_urandom. /dev/urandom can generate numbers fast
# But selecting numbers in ranges etc made for a fairly slow method
if [ -c /dev/urandom ] && get_command od; then
# Get a string of bytes from /dev/urandom using od
od -N 4 -A n -t uL /dev/urandom | tr -d '\n' | awk '{$1=$1+0};1'
# Otherwise we try to get another seed
# On some systems, 'date +%s' returns "%s", so we check for digits
elif date '+%s' >/dev/null 2>&1 | grep "[0-9]" >/dev/null 2>&1; then
date '+%s'
elif get_command perl; then
perl -e "print time"
# Last chance, this should work on almost anything
else
date | cksum | tr -d ' '
fi
}
# This function ensures equal distribution for methods that don't support it
# If you request random numbers between 1 and 10, you should only get a complete
# set of random numbers between 1 and 10. This is disabled with '-r'
# Note: for POSIX compat, we sadly can't use arrays
# this means there will be potentially a massive performance hit at scale
get_int_fill() {
# Call the method specified upon function invocation
# Use get_unique_ints to get an unsorted list of unique integers
intFill=$($1 | get_unique_ints)
# Count how many unique integers we have generated
intCount=$(printstr "${intFill}" | wc -l)
# If we've generated enough to satisfy nCount, then we just print them out
if [ "${intCount}" -ge "${nCount}" ]; then
printstr "${intFill}" | head -n "${nCount}"
# Otherwise, we walk through a few steps to try and quickly fill the gap
else
# Create a tmpdir
mkdir -p "${tmpDir}"
# Now dump what we have to a temporary file
printstr "${intFill}" > "${tmpDir}"/rnginit
# Until we have our full set of random integers,
# Keep throwing random ints into the temporary file
while [ "${intCount}" -lt "${nCount}" ]; do
eval "${1}" >> "${tmpDir}"/rnginit
intCount=$(get_unique_ints < "${tmpDir}"/rnginit | wc -l)
done
# Now that the full integer set is built, dump it out
get_unique_ints < "${tmpDir}"/rnginit | head -n "${nCount}"
fi
}
# Switch between get_int_fill or not
switch_repeat_mode() {
if [ "${repeat}" = true ]; then
"${1}"
else
get_int_fill "${1}"
fi
}
# This function allows us to print out unsorted, unique integers
get_unique_ints() {
# If 'awk' is available, we use it
if get_command awk; then
awk '!x[$0]++'
# Otherwise we use a double sort. This can be brutally slow at scale.
# We first prepend each line with a line number, then perform a unique sort
# on the second field (i.e. the generated integers), then we sort again on the
# line numbers to return the randomness and use cut to print out the integers
else
nl | sort -k 2 -u | sort | cut -f2
fi
}
# Setup the zeropad function, which converts numbers under 10 e.g. '9' becomes '09'
if [ -n "${zeroPad}" ]; then
print_zeropadding() {
# It appears that 'awk' is more portable vs sed 's/\<[0-9]\>/0&/'
if get_command awk; then
awk -v z="${zeroPad}" '{$1 = sprintf("%0*d", z, $1); print}'
elif get_command xargs; then
xargs printf '%0*d\n' "${zeroPad}"
else
die -e "'awk' or 'xargs' required for zero-padding, neither were found."
fi
}
else
print_zeropadding() {
cat -
}
fi
# Function to generate numbers using 'gawk'
get_int_gawk() {
gawk -v min="${nMin}" -v max="${nMax}" -v nNum="${nCount}" '
BEGIN{
srand(systime() + PROCINFO["pid"]);
i = 0;
while (i < nNum){
printf "%d\n", int(min+rand()*(max-min)); ++i
}
}
'
}
# Function to generate numbers using BSD 'jot'
get_int_jot() {
# Some versions of 'jot' have uniform distribution, others do not.
# See: https://unix.stackexchange.com/a/241199
# This is a lo-fi approach to try and cater for both
jot -w %i -r "${nCount}" "${nMin}" "$(( nMax + 1 ))" |
sed "s/$(( nMax + 1 ))/$(( nMax ))/"
}
# Function to generate numbers using 'nawk'
# This used to call 'mawk' but that was found to be prone to repeated output
get_int_nawk() {
nawk -v min="${nMin}" -v max="${nMax}" -v nNum="${nCount}" -v seed="$(get_seed)" '
BEGIN{
srand(seed); i = 0;
while (i < nNum){
printf "%d\n", int(min+rand()*(max-min)); ++i
}
}
'
}
# Function to generate numbers using 'perl'
get_int_perl() {
perl -le '
$mn=$ENV{nMin}; $mx=$ENV{nMax}; $cn=$ENV{nCount};
foreach my $i (1..$cn) {
printf "%.0f\n", int(rand($mx-$mn))+$mn ;
}
'
}
# Function to generate numbers using 'python'
# Other methods have been considered, check commit history if you're curious
get_int_python() {
python -c "for _ in xrange(${nCount}): import random; \
print random.randint(${nMin},${nMax})"
}
# Function to generate numbers primarily using '$RANDOM' special variable
# If '$RANDOM' isn't available (e.g. dash shell), we fall back to a BSD-style
# Linear congruential generator (LCG) which we use to create our own '$RANDOM'
# This way, the entire bitshifting formula remains the same
# See: https://rosettacode.org/wiki/Linear_congruential_generator
get_int_RANDOM() {
# We need to know the number of bits required to represent nMax (i.e. bitlength)
# This is for the rightwards bitshift
logn=1
nBitlen=0
while [ $((nMax - nMin)) -gt "${logn}" ] && [ "${logn}" -gt 0 ]; do
logn=$(( logn * 2 ))
nBitlen=$(( nBitlen + 1 ))
done
# We set the initial seed just in case we're using the LCG
rnSeed=$(get_seed)
# Start a loop based on nCount.
count=0
while [ "${count}" -lt "${nCount}" ]; do
# Start generating seeds for the LCG
rnSeed=$(( (1103515245 * rnSeed + 12345) % 2147483648 ))
# If the RANDOM variable is blank, we failover to the LCG
# We print as an unsigned integer to ensure that it's a positive number
# shellcheck disable=SC2169
RANDOM="${RANDOM:-$(printint -u "$(( rnSeed / 65536 ))")}"
# Set our initial bitlength
rndBitlen=15
# shellcheck disable=SC2169
rnd="${RANDOM}" # Capture one output sample of RANDOM
while [ "${rndBitlen}" -lt "${nBitlen}" ]; do
# Stir the seed again just in case
rnSeed=$(( (1103515245 * rnSeed + 12345) % 2147483648 ))
# If two invocations of RANDOM are the same, then we're working with a
# shell that does not support $RANDOM. So we use the LCG and rotate $RANDOM
# shellcheck disable=SC2169
if [ "${RANDOM}" = "${RANDOM}" ]; then
RANDOM=$(printint -u "$(( rnSeed / 65536 ))")
fi
# Bitshift RANDOM to the left to stack it i.e. 15 int -> 30 int -> 45 int etc
# shellcheck disable=SC2169
rnd=$(( rnd<<15|RANDOM ))
# Keep stacking until the while loop exits
rndBitlen=$(( rndBitlen + 15 ))
done
# Now bitshift it right
nRandShift=$(( rnd>>(rndBitlen-nBitlen) ))
# Next we test if the number we've generated fits into our range. If so,
# then we can use it and iterate the while loop
if [ $((nRandShift + nMin)) -le "${nMax}" ]; then
printint -u "$(( nRandShift + nMin ))"
count=$(( count + 1 ))
fi
done
}
# Function to generate numbers using GNU 'shuf'
get_int_shuf() {
# It turns out that Solaris 11 comes with 'shuf' v8.16, which lacks the
# '-r' option. This option was introduced in v8.22
# Once again, Solaris proves to be the bane of my scripting existence.
# First we test if the repeat option has been set, as this requires special handling
if [ "${repeat}" = true ]; then
# Test if 'shuf' can use '-r' and if so, use it
if shuf -n 1 -r -i 1-10 >/dev/null 2>&1; then
shuf -n "${nCount}" -r -i "${nMin}"-"${nMax}"
# Otherwise we assume that '-r' isn't available, and do it the old fashioned way
else
while [ "${nCount}" -gt 0 ]; do
shuf -n 1 -i "${nMin}"-"${nMax}"
# Decrement the counter
nCount=$(( nCount - 1 ))
done
fi
# If repeat isn't true, just do this
else
shuf -n "${nCount}" -i "${nMin}"-"${nMax}"
fi
}
# Function to attempt random integer generation using a textbook Vigna xorshift128+
# This RNG is also coupled with modulo de-biasing loosely from arc4random_uniform
get_int_xorshift128plus() {
# First, we need to calculate the range of integers we need to create
# i.e. convert from {x..y} to {0..n}
nRange=$(( nMax - nMin + 1 ))
# We attempt to de-bias our modulo as explained here and here and here:
# http://funloop.org/post/2013-07-12-generating-random-numbers-without-modulo-bias.html
# https://blog.hartwork.org/?p=2900
# https://medium.com/hownetworks/dont-waste-cycles-with-modulo-bias-35b6fdafcf94
# This calculation is careful to not invoke negative integer overflow
skipInts=$(( (maxRand - nRange) % nRange ))
# Our initial seed integers, one as per RFC 1149.5, the other from get_seed
int0=4
int1=$(get_seed)
# Start our count loop
count=0
while [ "${count}" -lt "${nCount}" ]; do
# xorshift128+ with preselected triples
int1=$(( int1 ^ int1 << 23 ))
int1=$(( int1 ^ int1 >> 17 ))
int1=$(( int1 ^ int0 ))
int1=$(( int1 ^ int0 >> 26 ))
seed1="${int1}"
# If our generated int is larger than the number of problematic ints
# Then we can modulo it safely, otherwise drop it and generate again
# By virtue of skipInts being >= 0, this will also drop generated negative overflow ints
# Then we simply add nMin to bring it back up into our desired range
if [ $(( (int0 + seed1) >= skipInts )) ]; then
printint -u "$(( ((int0 + seed1) % nRange) + nMin ))"
count=$(( count + 1 ))
fi
done
}
# Check if 'seq' is available, if not, provide a basic replacement function
# Note: this has been stripped back to cater only for ascending sequences
# A fuller, bash-friendly version is available at https://github.com/rawiriblundell
if ! get_command seq >/dev/null 2>&1; then
seq() {
i=$1
while [ "$i" -ne "$(( $2 + 1 ))" ]; do
printstr "$i"
i=$(( i + 1 ))
done
}
fi
###############################################################################
# Main
###############################################################################
main() {
# Cater for GNU shuf, nice and fast
# This function will also cater for the repeat option natively
if get_command shuf && get_shuf_type; then
print_debug shuf
get_int_shuf
# If we're on a BSD based host, likely 'jot' is available, so let's use it
# 'jot' is limited to 2^31-1 by the arc4random algorithm
# so we also test for an nMax limit based on that
elif get_command jot && [ "${nMax}" -le 2147483647 ]; then
print_debug jot
# Repeating generated numbers is the default behaviour of 'jot'
switch_repeat_mode get_int_jot
# Now we start going less-native and try perl. Very likely to be there,
# so very likely this will be a commonly used option
elif get_command perl; then
print_debug perl
switch_repeat_mode get_int_perl
# Otherwise, we try python
# We need to ensure that /dev/urandom is available, as python sources it
elif get_command python && [ -c /dev/urandom ]; then
print_debug python
switch_repeat_mode get_int_python
# No perl or python? Let's try 'gawk'
elif get_command gawk; then
print_debug gawk
switch_repeat_mode get_int_python
# No gawk? Surely 'nawk' is hanging around? Works very similar, but
# because we don't have systime() we have to replicate it as a seed for srand().
elif get_command nawk; then
print_debug nawk
switch_repeat_mode get_int_nawk
# Note: oawk does not have srand() or rand(),
# it's more trouble than it's worth so let's move on
# Next we try one of our own methods using a textbook xorshift128+
# coupled with a semi-not-so-textbook modulo debias method
elif [ $(( nCount ^ nCount )) -eq 0 ] >/dev/null 2>&1; then
print_debug xorshift128+
switch_repeat_mode get_int_xorshift128plus
# No shuf, jot, perl, python, gawk or nawk? AND xorshift isn't working?! Fear not!
# Let's try for a POSIX friendly shell solution. This is limited to 2^60-1
elif [ "${nMax}" -lt 1152921504606846975 ]; then
print_debug Skoruppa Bitshift
switch_repeat_mode get_int_RANDOM
# Provide an outright failure condition just in case
else
die -e "Unable to find a suitable method to generate a random integer"
fi | print_zeropadding
exit 0
}
# Call the main function
main