echo Verified current stable Not installed? Programming

Echo / Print Random Number

Print Random Number

Use 'echo $RANDOM' to generate a random number (0 to 32767) on Unix-based systems for testing scripts.

$
Terminal
echo $RANDOM

When To Use

Use when scripting requires randomness, such as generating test data or simulating variable user inputs in development environments. Suitable for scenarios where cryptographic strength is not necessary, but non-deterministic input is desirable, such as during fuzz testing.

Pro Tip

'$RANDOM' is a Bash-specific feature and depends on the shell. It's not suitable for cryptography. For secure random numbers, consider /dev/urandom.

Terminal Output

Expected runtime feedback

Simulated preview
>
Output
$ echo $RANDOM
21374

Anatomy of Output

Understanding the result

13415 Random Number

A pseudo-random integer between 0 and 32767 generated by Bash.

6001 Random Number

Successive calls to '$RANDOM' yield different results suited for simple randomness requirements.

32767 Max Value

The maximum possible output value from '$RANDOM', representing a 15-bit number.

0 Min Value

The minimum possible output value from '$RANDOM', encompassing full 15-bit range.

21234 Random Number

Output unaffected by previous states unless manipulated in custom scripts.

19876 Random Number

Illustrates the non-cryptographic nature and simplicity of Bash's random generation.

Power User Variants

Optimized versions

echo $(($RANDOM%100))

Generates a random number within a specific range (0-99). This uses modulo for range limitation, common in parameterized scripts.

Unix Pipeline

Shell combinations

echo $(($RANDOM+$SECONDS))

Adding '$SECONDS' ensures higher entropy during script execution for time-sensitive applications.

echo $RANDOM | xargs -n1

Use 'xargs' for processing multiple numbers, excellent for batch number generation in pipelines.

Troubleshooting

Common pitfalls

Command not found (bash: $RANDOM: command not found)

Solution: Use Bash shell, as '$RANDOM' is not supported in other shells like Zsh by default.

Inconsistent outputs across sessions

Solution: Ensure environment and shell settings remain constant, as '$RANDOM' bases its seed partially on time.

Unexpected repeated values

Solution: Check for accidental seeding within scripts that reset or influence '$RANDOM' state.

Command Breakdown

What each part is doing

echo
Base Command
The executable that performs this operation. Here it runs Echo before the shell applies any redirect operators.

How To Run

Execution path

  1. Step 1

    Run the command: echo $RANDOM

  2. Step 2

    Use the output in your testing script by integrating it directly.

Alternative Approaches

Comparable commands in other tools

Alternative programming tools for the same job.