Here is a solution that I did interactively in the bash shell. Each call will result in a random number whose value will fall in the range 0 to 9. Do this once for each digit.
Code:
echo $(( $RANDOM / 3276 ))
8
echo $(( $RANDOM / 3276 ))
6
echo $(( $RANDOM / 3276 ))
6
echo $(( $RANDOM / 3276 ))
7
echo $(( $RANDOM / 3276 ))
2
echo $(( $RANDOM / 3276 ))
4
echo $(( $RANDOM / 3276 ))
1
echo $(( $RANDOM / 3276 ))
2
echo $(( $RANDOM / 3276 ))
5
echo $(( $RANDOM / 3276 ))
7
echo $(( $RANDOM / 3276 ))
0
Okay, so that's just eleven digits.
You can put this in a shell script by putting the correct shell call at the top of the script and making the script executable.
Code:
#!/bin/bash
# This is a bash script to create a fourteen character string of random numeric digits.
echo -n "The random number is "
for ((i=1; i<15; i++)) ; do
echo -n $(( $RANDOM / 3276 ))
done
echo
You can store the random number in a variable as follows:
Code:
#!/bin/bash
# This is a bash script to create a fourteen character string of random numeric digits.
unset x
for ((i=1; i<15; i++)) ; do
x=$x$(( $RANDOM / 3276 ))
done
echo The random number is $x
echo
This is the theory behind this code.
The bash shell has a built in random number generator in the form of a shell variable named RANDOM. Each call to RANDOM will generate a random number in the value range of 0 to 32767. Since we want a value of 0 to 9 we have to divide the result of the call to RANDOM by 3276. This will result in an integer whose value will be in the range 0 to 9. This is the only really interesting part of the code. It is interesting because we are taking a tool that we are given that does what we want to do but the result is in the wrong range so we alter the result to fit our requirement. One potential problem with making this sort of an adjustment is that the code may fail to generate one or both of the values at the ends of the desired range. I have run this code a few dozen times and I have observed that both zeros and nines are generated. You always have to test your code. I have found that testing code with values that are at the extreme ends of the data set is critical. (In this case the data set is the range of digits from 0 to 9. The test is to see if these values are ever generated.) This is because of a common programming error referred to as an "off by one" error. Lots of code suffers from this problem and it can remain hidden unless you specifically test for it. If you run the program a bunch of times you should see some occurrences of zero and some occurrences of nine being generated. That means that our code is okay.
We have to put the dollar sign in front of the pseudo-variable named RANDOM so that we can read the value of this variable as per the rules of bash.
The for loop makes the code more compact. Each iteration of the for loop will generate one digit so we execute the loop 14 times to create 14 digits.
In the first example we print the result of each loop iteration immediately without allowing a line feed between each call to echo. This results in a single 14 digit number being printed on one line. In the second example we accumulate the results of each loop iteration in a variable named x and print the entire completed random number in one call to the echo function once the entire number has been created. The second approach has two advantages over the first approach. The second approach has less computer I/O so it is less work for the computer, and the second approach allows us to store the random number and use it elsewhere.
Happy random number generating.