Skip to content

Instantly share code, notes, and snippets.

@lpenaud
Last active August 21, 2024 16:23
Show Gist options
  • Select an option

  • Save lpenaud/184e1a766e7197b9c074742f18d85ca1 to your computer and use it in GitHub Desktop.

Select an option

Save lpenaud/184e1a766e7197b9c074742f18d85ca1 to your computer and use it in GitHub Desktop.
Bash random

Bash random

Functions utils to play with random number.

Examples

Get randomly a number between 0 to 10 or 5.

if rand::boolean; then
  echo "$(rand::next 10)"
else
  echo "$(rand::next 5)"
fi
#/bin/bash
# Functions utils to play with random number
# Return a random number from 0 to bound (exclusive).
# BOUND
function rand::next () {
echo $(( $RANDOM % $1 ))
}
# Return randomly true or false (1/0).
function rand::boolean () {
(( $RANDOM % 2 == 0 ))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment