Debian Codegraph @

seeding-random.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 # seeding-random.sh: Seeding the RANDOM variable.
3 # v 1.1, reldate 09 Feb 2013
4
5 MAXCOUNT=25 # How many numbers to generate.
6 SEED=
7
8 random_numbersscip-shell shell abs-guide 10-4 random_numbers(). ()
9 {
10 local count=0
11 local number
12
13 while [scip-shell system . . `[`. "$count" -lt "$MAXCOUNT" ]
14 do
15 number=$RANDOM
16 echoscip-shell system . . echo. -n "$number "
17 let "count++"
18 done
19 }
20
21 echoscip-shell system . . echo.; echoscip-shell system . . echo.
22
23 SEED=1
24 RANDOM=$SEED # Setting RANDOM seeds the random number generator.
25 echoscip-shell system . . echo. "Random seed = $SEED"
26 random_numbersscip-shell shell abs-guide 10-4 random_numbers().
27
28
29 RANDOM=$SEED # Same seed for RANDOM . . .
30 echoscip-shell system . . echo.; echoscip-shell system . . echo. "Again, with same random seed ..."
31 echoscip-shell system . . echo. "Random seed = $SEED"
32 random_numbersscip-shell shell abs-guide 10-4 random_numbers(). # . . . reproduces the exact same number series.
33 #
34 # When is it useful to duplicate a "random" series?
35
36 echoscip-shell system . . echo.; echoscip-shell system . . echo.
37
38 SEED=2
39 RANDOM=$SEED # Trying again, but with a different seed . . .
40 echoscip-shell system . . echo. "Random seed = $SEED"
41 random_numbersscip-shell shell abs-guide 10-4 random_numbers(). # . . . gives a different number series.
42
43 echoscip-shell system . . echo.; echoscip-shell system . . echo.
44
45 # RANDOM=$$ seeds RANDOM from process id of script.
46 # It is also possible to seed RANDOM from 'time' or 'date' commands.
47
48 # Getting fancy...
49 SEED=$(headscip-shell system . . head. -1 /dev/urandomscip-shell filesystem . . `/dev/urandom`. | odscip-shell system . . od. -N 1 | awkscip-shell system . . awk. '{ print $2 }'| sedscip-shell system . . sed. s/^0*//)
50 # Pseudo-random output fetched
51 #+ from /dev/urandom (system pseudo-random device-file),
52 #+ then converted to line of printable (octal) numbers by "od",
53 #+ then "awk" retrieves just one number for SEED,
54 #+ finally "sed" removes any leading zeros.
55 RANDOM=$SEED
56 echoscip-shell system . . echo. "Random seed = $SEED"
57 random_numbersscip-shell shell abs-guide 10-4 random_numbers().
58
59 echoscip-shell system . . echo.; echoscip-shell system . . echo.
60
61 exit 0