'Generate a Random Output from the List of Multiplicities of a Number X up to a MAX Number Y?

Using a bash script, I want to choose a random number from such a list: [0, X, 2X, 3X, 4X, ..., Y] where Y=k*X and k is an integer. X and Y are just some input numbers by the user. To clarify the question, here is an example:

X=2
#Y=10*X
Y=20
# The <desired_command> to choose a random number from [0, 2, 4, 6, 8, ..., 20] --> sample output=12
desired_command

How can I do this?



Solution 1:[1]

You can use this command:

echo $(($(shuf -i 0-$((Y/X)) -n1)*X))

Solution 2:[2]

# x positive integer
# y positive integer y >= x
x=11
y=34
echo $(((RANDOM % (y/x+1)) * x))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Kamoo