Mike
Mike

Reputation: 189

Shell Scripting: How to choose a randow number from three values?

I want to write a Shell Script. The task is, that I have to pick a random number. But the numbers must be one of the following three. 5,10,15 So if "let's say" 'a' is smaller than 5, I have to pick randomly a number of 5,10 or 15. How can I do that?

Upvotes: 0

Views: 110

Answers (2)

jsalatas
jsalatas

Reputation: 255

Try the following

#!/bin/bash

arr=(5 10 15);
picked_element=${arr[$(($RANDOM % 3))]};
echo $picked_element;

Upvotes: 0

Val Berthe
Val Berthe

Reputation: 2054

Here's a hint to help you do this.

First, create a variable (array) containing your 3 values : arr = (5 10 15).

Then, create a random number called index and floor it to 2.

Finally, retrieve the number ${arr[$index]}.

Upvotes: 1

Related Questions