19

Is it possible to have variable which picks a random number from three pre-decided numbers?

Sample:

var= 10 or 100 or 1000
1
  • 1
    I do not program in UNIX, so I cannot write the code, but here is another approach: val = left("1000",length = Random 2 through 4). To have three unrelated values, the code would be similar. Dec 16, 2016 at 20:58

5 Answers 5

31

Use an array to hold the values and choose among them using the built-in variable $RANDOM. For example,

x[0]=10     # One decade
x[1]=100    # One century
x[2]=1000   # One millennium

for ((i=1; i < 20; ++i)); do echo -n " ${x[$RANDOM%3]}"; done; echo
1000 10 10 10 10 100 10 100 100 10 10 100 100 100 10 1000 1000 1000 10

The quality of randomness won't be the best possible (read bytes from /dev/urandom for that), but it should be more than good enough for a script.

Note 1: As people have observed in the comments, instead of initializing the array elements individually one can of course use an array litteral: x=(10 100 1000).

Note 2: Instead of hard-coding the number of elements in the array, a radom element can be extracted by ${x[$RANDOM%${#x[@]}]}.

7
  • 5
    +1 While zeppelin's answer is clever in this particular case, the array approach is so much more general. Dec 16, 2016 at 8:17
  • 4
    Seems like x=(10 100 1000) creates an array from a literal in bash, this would be more readable and more idiomatic
    – cat
    Dec 16, 2016 at 13:11
  • 1
    @cat: Opinion on readability vary. I like to put individual items on separate lines, usually with trailing comments explaining the value.
    – AlexP
    Dec 16, 2016 at 13:20
  • @AlexP Oh, I guess I wouldn't comment if I thought it was on purpose. fair enough though
    – cat
    Dec 16, 2016 at 13:23
  • 1
    @AlexP x[0]=10 # This sets the first value of x to ten. Ten is the number of fingers that humans have (excepting birth defects or mutilation). It is also the sum of 9 and 1, or product of 5 and 2. Ten is not prime. A traditional Christmas gift is ten lords a-leaping. something like that?
    – Nick T
    Dec 16, 2016 at 20:18
29

If you are using bash (or zsh or ksh93) you can just do:

echo "$((10**($RANDOM%3+1)))"

or

var=$((10**($RANDOM%3+1)))

to assign it to var

1
  • :). Easy when you see it, but I didnt. Thanks!! Dec 16, 2016 at 15:47
20

You can also use the GNU coreutils shuf utility:

a=$(shuf -n1 -e 10 100 1000)

Using RANDOM as per the other answers is almost certainly faster though.

1
8
case $(( RANDOM % 3 )) in
    0)
        var=10
        ;;
    1)
        var=100
        ;;
    2)
        var=1000
        ;;
esac
0
5

Here's a slightly cryptic way:

printf -v var '1%0*d' $(( RANDOM % 3 + 1 )) 0

This will assign the random value to $var as required. The printf format string is 1%0Nd - this causes 0 to be printed with N leading zeroes, where N will be a random integer in the interval [1,3].

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .