| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # random2.sh: Returns a pseudorandom number in the range 0 - 1, |
| 3 | #+ to 6 decimal places. For example: 0.822725 |
| 4 | # Uses the awk rand() function. |
| 5 | |
| 6 | AWKSCRIPT=' { srand(); print rand() } ' |
| 7 | # Command(s)/parameters passed to awk |
| 8 | # Note that srand() reseeds awk's random number generator. |
| 9 | |
| 10 | |
| 11 | echoscip-shell system . . echo. -n "Random number between 0 and 1 = " |
| 12 | |
| 13 | echoscip-shell system . . echo. | awkscip-shell system . . awk. "$AWKSCRIPT" |
| 14 | # What happens if you leave out the 'echo'? |
| 15 | |
| 16 | exit 0 |
| 17 | |
| 18 | |
| 19 | # Exercises: |
| 20 | # --------- |
| 21 | |
| 22 | # 1) Using a loop construct, print out 10 different random numbers. |
| 23 | # (Hint: you must reseed the srand() function with a different seed |
| 24 | #+ in each pass through the loop. What happens if you omit this?) |
| 25 | |
| 26 | # 2) Using an integer multiplier as a scaling factor, generate random numbers |
| 27 | #+ in the range of 10 to 100. |
| 28 | |
| 29 | # 3) Same as exercise #2, above, but generate random integers this time. |