Skip to content

Instantly share code, notes, and snippets.

@dbrady
Created April 6, 2012 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbrady/2317393 to your computer and use it in GitHub Desktop.
Save dbrady/2317393 to your computer and use it in GitHub Desktop.
Rand using bc
#!/bin/sh
# Linear Congruential Generator using bc
#
# Note the shenanigans necessary to seed the generator with the epoch
# time: $(date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s") does nothing
# more than return the current date in epoch seconds, which we use to
# seed the generator. This generator is due to Knuth, and cited in
# Numerical Recipes in C, chapter 7. It's fast on old 32-bit machines
# but not a very good RNG:
echo "(1664525*($(date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s")%2^32)+1013904223)%2^32" | bc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment