How to resolve the algorithm Pick random element step by step in the Bash programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pick random element step by step in the Bash programming language

Table of Contents

Problem Statement

Demonstrate how to pick a random element from a list.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pick random element step by step in the Bash programming language

Source code in the bash programming language

# borrowed from github.com/search?q=bashnative

rand() {
	printf $((  $1 *  RANDOM  / 32767   ))
}
rand_element () {
    local -a th=("$@")
    unset th[0]
    printf $'%s\n' "${th[$(($(rand "${#th[*]}")+1))]}"
}

echo "You feel like a $(rand_element pig donkey unicorn eagle) today"


  

You may also check:How to resolve the algorithm Array concatenation step by step in the bash programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the bash programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the Bash programming language
You may also check:How to resolve the algorithm Leap year step by step in the Bash programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the bash programming language