Random Variable

Hi,

Could you please let me know what the following code does, I know that it means generating random numbers, however not sure what is the entire purpose.

  R=$(($RANDOM % 2))
        delay=$(($RANDOM % 10))

        if [ $R -gt 0 ]
        then
                TEXT='X'
        else
                TEXT='F'
        fi


Thanks.

Line 1 of YOUR code sets an integer random number R variable to 0 OR 1 dependant on the program run(time).
Line 2 does the same between 0 to 9 but in this code is wasted time as it effectively is not used and therefore does nothing...

The conditional statement looks for 0 as the crucial value from the variable R and sets the TEXT variable to "F".

ANY other value would give the TEXT variable as "X"...

#!/bin/bash

R=$(($RANDOM % 2))
echo "$R"
delay=$(($RANDOM % 10))
echo "$delay"
        if [ $R -gt 0 ]
        then
                TEXT='X'
                echo "$TEXT"
        else
                TEXT='F'
                echo "$TEXT"
        fi

I hope that is lucid enough...

1 Like

it is just a digital version of a coin flip ... depending on the remainder after the random number generated is divided by 2, the code sets the value of the TEXT variable to X if the resulting number is greater than 0 or sets it to F if not ...

1 Like

Thanks a Lot!