diff --git a/README.md b/README.md index 73b05f8..f168339 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ Any donations are appreciated, they give me the time to make this the best resou * [Arrays](#arrays) * [Reverse an array.](#reverse-an-array) * [Remove duplicate array elements.](#remove-duplicate-array-elements) + * [Random array element.](#random-array-element) * [Cycle through an array.](#cycle-through-an-array) * [Toggle between two values.](#toggle-between-two-values) * [Loops](#loops) @@ -570,6 +571,30 @@ green blue ``` +## Random array element. + +**Example Function:** + +```sh +random_array_element() { + # Usage: random_array_element "array" + arr=("$@") + printf '%s\n' "${arr[RANDOM % $#]}" +} +``` + +**Example Usage:** + +```shell +$ array=(red green blue yellow brown) +$ random_array_element "${array[@]}" +yellow + +# You can also just pass multiple arguments. +$ random_array_element 1 2 3 4 5 6 7 +3 +``` + ## Cycle through an array. Each time the `printf` is called, the next array element is printed. When