Thursday, March 22, 2012

[alvdcsis] random sleep

A quick and dirty way to sleep for a bounded random amount of time in shell scripts, using the bash shell's variable $RANDOM which returns a random integer value between 0 and 32767. (Truly a "random variable" in the mathematical sense.)

The bound mentioned in the comment is inclusive, that is, (for example) up to and including 1 second, so either 0 or 1 second. The granularity is minimum 1 second (as required by expr's integer arithmetic but not (incidentally) required by sleep which can take a fractional argument) but can be considerably larger: for example, 16 minutes for the case of waiting up to 1 year, which is on the edge of absurdity assuming you can keep your computer continuously running for that long). Combine successive calls to approximate fine granularity over a long period.

Source code for the generator (written in a mix of Haskell and Perl) is here. An example use of ceilDiv. The large multiplier 281474976710656 is 2^48, which makes the sampling more uniform and is the largest multiplier that avoids overflowing the 63-bit arithmetic of the expr command.

# sleep up to 1 second
sleep $(expr $RANDOM / 16384)

# sleep up to 2 seconds
sleep $(expr 281474976710656 \* $RANDOM / 3074457345618258603)

# sleep up to 5 seconds
sleep $(expr 281474976710656 \* $RANDOM / 1537228672809129302)

# sleep up to 10 seconds
sleep $(expr 281474976710656 \* $RANDOM / 838488366986797801)

# sleep up to 15 seconds
sleep $(expr $RANDOM / 2048)

# sleep up to 20 seconds
sleep $(expr 281474976710656 \* $RANDOM / 439208192231179801)

# sleep up to 30 seconds
sleep $(expr 281474976710656 \* $RANDOM / 297528130221121801)

# sleep up to 40 seconds
sleep $(expr 281474976710656 \* $RANDOM / 224960293581823801)

# sleep up to 45 seconds
sleep $(expr 281474976710656 \* $RANDOM / 200508087757712518)

# sleep up to 1 minute
sleep $(expr 281474976710656 \* $RANDOM / 151202820276307801)

# sleep up to 2 minutes
sleep $(expr 281474976710656 \* $RANDOM / 76226215180617982)

# sleep up to 5 minutes
sleep $(expr 281474976710656 \* $RANDOM / 30642432016128824)

# sleep up to 10 minutes
sleep $(expr 281474976710656 \* $RANDOM / 15346708879958030)

# sleep up to 15 minutes
sleep $(expr 281474976710656 \* $RANDOM / 10236816911048586)

# sleep up to 20 minutes
sleep $(expr 281474976710656 \* $RANDOM / 7679743577730871)

# sleep up to 30 minutes
sleep $(expr 281474976710656 \* $RANDOM / 5121250436898821)

# sleep up to 40 minutes
sleep $(expr 281474976710656 \* $RANDOM / 3841471069077375)

# sleep up to 45 minutes
sleep $(expr 281474976710656 \* $RANDOM / 3414798976991772)

# sleep up to 1 hour
sleep $(expr 281474976710656 \* $RANDOM / 2561336305708075)

# sleep up to 2 hours
sleep $(expr 281474976710656 \* $RANDOM / 1280845998730007)

# sleep up to 3 hours
sleep $(expr 281474976710656 \* $RANDOM / 853936861110525)

# sleep up to 4 hours
sleep $(expr 281474976710656 \* $RANDOM / 640467470096159)

# sleep up to 6 hours
sleep $(expr 281474976710656 \* $RANDOM / 426988196697134)

# sleep up to 8 hours
sleep $(expr 281474976710656 \* $RANDOM / 320244853888920)

# sleep up to 9 hours
sleep $(expr 281474976710656 \* $RANDOM / 284663190545193)

# sleep up to 12 hours
sleep $(expr 281474976710656 \* $RANDOM / 213499040227189)

# sleep up to 16 hours
sleep $(expr 281474976710656 \* $RANDOM / 160125206799445)

# sleep up to 18 hours
sleep $(expr 281474976710656 \* $RANDOM / 142333791713937)

# sleep up to 1 day
sleep $(expr 281474976710656 \* $RANDOM / 106750755626148)

# sleep up to 2 days
sleep $(expr 281474976710656 \* $RANDOM / 53375686696575)

# sleep up to 3 days
sleep $(expr 281474976710656 \* $RANDOM / 35583859772358)

# sleep up to 4 days
sleep $(expr 281474976710656 \* $RANDOM / 26687920569833)

# sleep up to 5 days
sleep $(expr 281474976710656 \* $RANDOM / 21350348811357)

# sleep up to 6 days
sleep $(expr 281474976710656 \* $RANDOM / 17791964206965)

# sleep up to 1 week
sleep $(expr 281474976710656 \* $RANDOM / 15250259237096)

# sleep up to 2 weeks
sleep $(expr 281474976710656 \* $RANDOM / 7625135922387)

# sleep up to 3 weeks
sleep $(expr 281474976710656 \* $RANDOM / 5083425349113)

# sleep up to 4 weeks
sleep $(expr 281474976710656 \* $RANDOM / 3812569537155)

# sleep up to 30 days
sleep $(expr 281474976710656 \* $RANDOM / 3558398332738)

# sleep up to 60 days
sleep $(expr 281474976710656 \* $RANDOM / 1779199509579)

# sleep up to 61 days
sleep $(expr 281474976710656 \* $RANDOM / 1750032310038)

# sleep up to 90 days
sleep $(expr 281474976710656 \* $RANDOM / 1186133082655)

# sleep up to 91 days
sleep $(expr 281474976710656 \* $RANDOM / 1173098654833)

# sleep up to 180 days
sleep $(expr 281474976710656 \* $RANDOM / 593066579462)

# sleep up to 182 days
sleep $(expr 281474976710656 \* $RANDOM / 586549364718)

# sleep up to 240 days
sleep $(expr 281474976710656 \* $RANDOM / 444799941747)

# sleep up to 243 days
sleep $(expr 281474976710656 \* $RANDOM / 439308584703)

# sleep up to 270 days
sleep $(expr 281474976710656 \* $RANDOM / 395377728116)

# sleep up to 274 days
sleep $(expr 281474976710656 \* $RANDOM / 389605790723)

# sleep up to 360 days
sleep $(expr 281474976710656 \* $RANDOM / 296533299265)

# sleep up to 365 days
sleep $(expr 281474976710656 \* $RANDOM / 292471199404)

No comments :