| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >$RANDOM: generate random integer</TITLE |
| 6 | ><META |
| 7 | NAME="GENERATOR" |
| 8 | CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ |
| 9 | "><LINK |
| 10 | REL="HOME" |
| 11 | TITLE="Advanced Bash-Scripting Guide" |
| 12 | HREF="index.html"><LINK |
| 13 | REL="UP" |
| 14 | TITLE="Another Look at Variables" |
| 15 | HREF="variables2.html"><LINK |
| 16 | REL="PREVIOUS" |
| 17 | TITLE="Typing variables: declare or |
| 18 | typeset" |
| 19 | HREF="declareref.html"><LINK |
| 20 | REL="NEXT" |
| 21 | TITLE="Manipulating Variables" |
| 22 | HREF="manipulatingvars.html"><META |
| 23 | HTTP-EQUIV="Content-Style-Type" |
| 24 | CONTENT="text/css"><LINK |
| 25 | REL="stylesheet" |
| 26 | HREF="common/kde-common.css" |
| 27 | TYPE="text/css"><META |
| 28 | HTTP-EQUIV="Content-Type" |
| 29 | CONTENT="text/html; charset=iso-8859-1"><META |
| 30 | HTTP-EQUIV="Content-Language" |
| 31 | CONTENT="en"><LINK |
| 32 | REL="stylesheet" |
| 33 | HREF="common/kde-localised.css" |
| 34 | TYPE="text/css" |
| 35 | TITLE="KDE-English"><LINK |
| 36 | REL="stylesheet" |
| 37 | HREF="common/kde-default.css" |
| 38 | TYPE="text/css" |
| 39 | TITLE="KDE-Default"></HEAD |
| 40 | ><BODY |
| 41 | CLASS="SECT1" |
| 42 | BGCOLOR="#FFFFFF" |
| 43 | TEXT="#000000" |
| 44 | LINK="#AA0000" |
| 45 | VLINK="#AA0055" |
| 46 | ALINK="#AA0000" |
| 47 | STYLE="font-family: sans-serif;" |
| 48 | ><DIV |
| 49 | CLASS="NAVHEADER" |
| 50 | ><TABLE |
| 51 | SUMMARY="Header navigation table" |
| 52 | WIDTH="100%" |
| 53 | BORDER="0" |
| 54 | CELLPADDING="0" |
| 55 | CELLSPACING="0" |
| 56 | ><TR |
| 57 | ><TH |
| 58 | COLSPAN="3" |
| 59 | ALIGN="center" |
| 60 | >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH |
| 61 | ></TR |
| 62 | ><TR |
| 63 | ><TD |
| 64 | WIDTH="10%" |
| 65 | ALIGN="left" |
| 66 | VALIGN="bottom" |
| 67 | ><A |
| 68 | HREF="declareref.html" |
| 69 | ACCESSKEY="P" |
| 70 | >Prev</A |
| 71 | ></TD |
| 72 | ><TD |
| 73 | WIDTH="80%" |
| 74 | ALIGN="center" |
| 75 | VALIGN="bottom" |
| 76 | >Chapter 9. Another Look at Variables</TD |
| 77 | ><TD |
| 78 | WIDTH="10%" |
| 79 | ALIGN="right" |
| 80 | VALIGN="bottom" |
| 81 | ><A |
| 82 | HREF="manipulatingvars.html" |
| 83 | ACCESSKEY="N" |
| 84 | >Next</A |
| 85 | ></TD |
| 86 | ></TR |
| 87 | ></TABLE |
| 88 | ><HR |
| 89 | ALIGN="LEFT" |
| 90 | WIDTH="100%"></DIV |
| 91 | ><DIV |
| 92 | CLASS="SECT1" |
| 93 | ><H1 |
| 94 | CLASS="SECT1" |
| 95 | ><A |
| 96 | NAME="RANDOMVAR" |
| 97 | ></A |
| 98 | >9.3. $RANDOM: generate random integer</H1 |
| 99 | ><TABLE |
| 100 | BORDER="0" |
| 101 | WIDTH="100%" |
| 102 | CELLSPACING="0" |
| 103 | CELLPADDING="0" |
| 104 | CLASS="EPIGRAPH" |
| 105 | ><TR |
| 106 | ><TD |
| 107 | WIDTH="45%" |
| 108 | > </TD |
| 109 | ><TD |
| 110 | WIDTH="45%" |
| 111 | ALIGN="LEFT" |
| 112 | VALIGN="TOP" |
| 113 | ><I |
| 114 | ><P |
| 115 | ><I |
| 116 | >Anyone who attempts to generate random numbers by |
| 117 | deterministic means is, of course, living in a state of |
| 118 | sin.</I |
| 119 | ></P |
| 120 | ><P |
| 121 | ><I |
| 122 | >--John von Neumann</I |
| 123 | ></P |
| 124 | ></I |
| 125 | ></TD |
| 126 | ></TR |
| 127 | ></TABLE |
| 128 | ><P |
| 129 | ><A |
| 130 | NAME="RANDOMVAR01" |
| 131 | ></A |
| 132 | ></P |
| 133 | ><P |
| 134 | ><TT |
| 135 | CLASS="VARNAME" |
| 136 | >$RANDOM</TT |
| 137 | > is an internal Bash <A |
| 138 | HREF="functions.html#FUNCTIONREF" |
| 139 | >function</A |
| 140 | > (not a constant) that |
| 141 | returns a <I |
| 142 | CLASS="FIRSTTERM" |
| 143 | >pseudorandom</I |
| 144 | > |
| 145 | |
| 146 | <A |
| 147 | NAME="AEN5817" |
| 148 | HREF="#FTN.AEN5817" |
| 149 | >[1]</A |
| 150 | > |
| 151 | |
| 152 | integer in the range 0 - 32767. It should |
| 153 | <TT |
| 154 | CLASS="REPLACEABLE" |
| 155 | ><I |
| 156 | >not</I |
| 157 | ></TT |
| 158 | > be used to generate an encryption |
| 159 | key.</P |
| 160 | ><DIV |
| 161 | CLASS="EXAMPLE" |
| 162 | ><HR><A |
| 163 | NAME="EX21" |
| 164 | ></A |
| 165 | ><P |
| 166 | ><B |
| 167 | >Example 9-11. Generating random numbers</B |
| 168 | ></P |
| 169 | ><TABLE |
| 170 | BORDER="0" |
| 171 | BGCOLOR="#E0E0E0" |
| 172 | WIDTH="100%" |
| 173 | ><TR |
| 174 | ><TD |
| 175 | ><PRE |
| 176 | CLASS="PROGRAMLISTING" |
| 177 | > 1 #!/bin/bash |
| 178 | 2 |
| 179 | 3 # $RANDOM returns a different random integer at each invocation. |
| 180 | 4 # Nominal range: 0 - 32767 (signed 16-bit integer). |
| 181 | 5 |
| 182 | 6 MAXCOUNT=10 |
| 183 | 7 count=1 |
| 184 | 8 |
| 185 | 9 echo |
| 186 | 10 echo "$MAXCOUNT random numbers:" |
| 187 | 11 echo "-----------------" |
| 188 | 12 while [ "$count" -le $MAXCOUNT ] # Generate 10 ($MAXCOUNT) random integers. |
| 189 | 13 do |
| 190 | 14 number=$RANDOM |
| 191 | 15 echo $number |
| 192 | 16 let "count += 1" # Increment count. |
| 193 | 17 done |
| 194 | 18 echo "-----------------" |
| 195 | 19 |
| 196 | 20 # If you need a random int within a certain range, use the 'modulo' operator. |
| 197 | 21 # This returns the remainder of a division operation. |
| 198 | 22 |
| 199 | 23 RANGE=500 |
| 200 | 24 |
| 201 | 25 echo |
| 202 | 26 |
| 203 | 27 number=$RANDOM |
| 204 | 28 let "number %= $RANGE" |
| 205 | 29 # ^^ |
| 206 | 30 echo "Random number less than $RANGE --- $number" |
| 207 | 31 |
| 208 | 32 echo |
| 209 | 33 |
| 210 | 34 |
| 211 | 35 |
| 212 | 36 # If you need a random integer greater than a lower bound, |
| 213 | 37 #+ then set up a test to discard all numbers below that. |
| 214 | 38 |
| 215 | 39 FLOOR=200 |
| 216 | 40 |
| 217 | 41 number=0 #initialize |
| 218 | 42 while [ "$number" -le $FLOOR ] |
| 219 | 43 do |
| 220 | 44 number=$RANDOM |
| 221 | 45 done |
| 222 | 46 echo "Random number greater than $FLOOR --- $number" |
| 223 | 47 echo |
| 224 | 48 |
| 225 | 49 # Let's examine a simple alternative to the above loop, namely |
| 226 | 50 # let "number = $RANDOM + $FLOOR" |
| 227 | 51 # That would eliminate the while-loop and run faster. |
| 228 | 52 # But, there might be a problem with that. What is it? |
| 229 | 53 |
| 230 | 54 |
| 231 | 55 |
| 232 | 56 # Combine above two techniques to retrieve random number between two limits. |
| 233 | 57 number=0 #initialize |
| 234 | 58 while [ "$number" -le $FLOOR ] |
| 235 | 59 do |
| 236 | 60 number=$RANDOM |
| 237 | 61 let "number %= $RANGE" # Scales $number down within $RANGE. |
| 238 | 62 done |
| 239 | 63 echo "Random number between $FLOOR and $RANGE --- $number" |
| 240 | 64 echo |
| 241 | 65 |
| 242 | 66 |
| 243 | 67 |
| 244 | 68 # Generate binary choice, that is, "true" or "false" value. |
| 245 | 69 BINARY=2 |
| 246 | 70 T=1 |
| 247 | 71 number=$RANDOM |
| 248 | 72 |
| 249 | 73 let "number %= $BINARY" |
| 250 | 74 # Note that let "number >>= 14" gives a better random distribution |
| 251 | 75 #+ (right shifts out everything except last binary digit). |
| 252 | 76 if [ "$number" -eq $T ] |
| 253 | 77 then |
| 254 | 78 echo "TRUE" |
| 255 | 79 else |
| 256 | 80 echo "FALSE" |
| 257 | 81 fi |
| 258 | 82 |
| 259 | 83 echo |
| 260 | 84 |
| 261 | 85 |
| 262 | 86 # Generate a toss of the dice. |
| 263 | 87 SPOTS=6 # Modulo 6 gives range 0 - 5. |
| 264 | 88 # Incrementing by 1 gives desired range of 1 - 6. |
| 265 | 89 # Thanks, Paulo Marcel Coelho Aragao, for the simplification. |
| 266 | 90 die1=0 |
| 267 | 91 die2=0 |
| 268 | 92 # Would it be better to just set SPOTS=7 and not add 1? Why or why not? |
| 269 | 93 |
| 270 | 94 # Tosses each die separately, and so gives correct odds. |
| 271 | 95 |
| 272 | 96 let "die1 = $RANDOM % $SPOTS +1" # Roll first one. |
| 273 | 97 let "die2 = $RANDOM % $SPOTS +1" # Roll second one. |
| 274 | 98 # Which arithmetic operation, above, has greater precedence -- |
| 275 | 99 #+ modulo (%) or addition (+)? |
| 276 | 100 |
| 277 | 101 |
| 278 | 102 let "throw = $die1 + $die2" |
| 279 | 103 echo "Throw of the dice = $throw" |
| 280 | 104 echo |
| 281 | 105 |
| 282 | 106 |
| 283 | 107 exit 0</PRE |
| 284 | ></TD |
| 285 | ></TR |
| 286 | ></TABLE |
| 287 | ><HR></DIV |
| 288 | ><DIV |
| 289 | CLASS="EXAMPLE" |
| 290 | ><HR><A |
| 291 | NAME="PICKCARD" |
| 292 | ></A |
| 293 | ><P |
| 294 | ><B |
| 295 | >Example 9-12. Picking a random card from a deck</B |
| 296 | ></P |
| 297 | ><TABLE |
| 298 | BORDER="0" |
| 299 | BGCOLOR="#E0E0E0" |
| 300 | WIDTH="100%" |
| 301 | ><TR |
| 302 | ><TD |
| 303 | ><PRE |
| 304 | CLASS="PROGRAMLISTING" |
| 305 | > 1 #!/bin/bash |
| 306 | 2 # pick-card.sh |
| 307 | 3 |
| 308 | 4 # This is an example of choosing random elements of an array. |
| 309 | 5 |
| 310 | 6 |
| 311 | 7 # Pick a card, any card. |
| 312 | 8 |
| 313 | 9 Suites="Clubs |
| 314 | 10 Diamonds |
| 315 | 11 Hearts |
| 316 | 12 Spades" |
| 317 | 13 |
| 318 | 14 Denominations="2 |
| 319 | 15 3 |
| 320 | 16 4 |
| 321 | 17 5 |
| 322 | 18 6 |
| 323 | 19 7 |
| 324 | 20 8 |
| 325 | 21 9 |
| 326 | 22 10 |
| 327 | 23 Jack |
| 328 | 24 Queen |
| 329 | 25 King |
| 330 | 26 Ace" |
| 331 | 27 |
| 332 | 28 # Note variables spread over multiple lines. |
| 333 | 29 |
| 334 | 30 |
| 335 | 31 suite=($Suites) # Read into array variable. |
| 336 | 32 denomination=($Denominations) |
| 337 | 33 |
| 338 | 34 num_suites=${#suite[*]} # Count how many elements. |
| 339 | 35 num_denominations=${#denomination[*]} |
| 340 | 36 |
| 341 | 37 echo -n "${denomination[$((RANDOM%num_denominations))]} of " |
| 342 | 38 echo ${suite[$((RANDOM%num_suites))]} |
| 343 | 39 |
| 344 | 40 |
| 345 | 41 # $bozo sh pick-cards.sh |
| 346 | 42 # Jack of Clubs |
| 347 | 43 |
| 348 | 44 |
| 349 | 45 # Thank you, "jipe," for pointing out this use of $RANDOM. |
| 350 | 46 exit 0</PRE |
| 351 | ></TD |
| 352 | ></TR |
| 353 | ></TABLE |
| 354 | ><HR></DIV |
| 355 | ><P |
| 356 | ><A |
| 357 | NAME="BROWNIANREF" |
| 358 | ></A |
| 359 | ></P |
| 360 | ><DIV |
| 361 | CLASS="EXAMPLE" |
| 362 | ><HR><A |
| 363 | NAME="BROWNIAN" |
| 364 | ></A |
| 365 | ><P |
| 366 | ><B |
| 367 | >Example 9-13. Brownian Motion Simulation</B |
| 368 | ></P |
| 369 | ><TABLE |
| 370 | BORDER="0" |
| 371 | BGCOLOR="#E0E0E0" |
| 372 | WIDTH="100%" |
| 373 | ><TR |
| 374 | ><TD |
| 375 | ><PRE |
| 376 | CLASS="PROGRAMLISTING" |
| 377 | > 1 #!/bin/bash |
| 378 | 2 # brownian.sh |
| 379 | 3 # Author: Mendel Cooper |
| 380 | 4 # Reldate: 10/26/07 |
| 381 | 5 # License: GPL3 |
| 382 | 6 |
| 383 | 7 # ---------------------------------------------------------------- |
| 384 | 8 # This script models Brownian motion: |
| 385 | 9 #+ the random wanderings of tiny particles in a fluid, |
| 386 | 10 #+ as they are buffeted by random currents and collisions. |
| 387 | 11 #+ This is colloquially known as the "Drunkard's Walk." |
| 388 | 12 |
| 389 | 13 # It can also be considered as a stripped-down simulation of a |
| 390 | 14 #+ Galton Board, a slanted board with a pattern of pegs, |
| 391 | 15 #+ down which rolls a succession of marbles, one at a time. |
| 392 | 16 #+ At the bottom is a row of slots or catch basins in which |
| 393 | 17 #+ the marbles come to rest at the end of their journey. |
| 394 | 18 # Think of it as a kind of bare-bones Pachinko game. |
| 395 | 19 # As you see by running the script, |
| 396 | 20 #+ most of the marbles cluster around the center slot. |
| 397 | 21 #+ This is consistent with the expected binomial distribution. |
| 398 | 22 # As a Galton Board simulation, the script |
| 399 | 23 #+ disregards such parameters as |
| 400 | 24 #+ board tilt-angle, rolling friction of the marbles, |
| 401 | 25 #+ angles of impact, and elasticity of the pegs. |
| 402 | 26 # To what extent does this affect the accuracy of the simulation? |
| 403 | 27 # ---------------------------------------------------------------- |
| 404 | 28 |
| 405 | 29 PASSES=500 # Number of particle interactions / marbles. |
| 406 | 30 ROWS=10 # Number of "collisions" (or horiz. peg rows). |
| 407 | 31 RANGE=3 # 0 - 2 output range from $RANDOM. |
| 408 | 32 POS=0 # Left/right position. |
| 409 | 33 RANDOM=$$ # Seeds the random number generator from PID |
| 410 | 34 #+ of script. |
| 411 | 35 |
| 412 | 36 declare -a Slots # Array holding cumulative results of passes. |
| 413 | 37 NUMSLOTS=21 # Number of slots at bottom of board. |
| 414 | 38 |
| 415 | 39 |
| 416 | 40 Initialize_Slots () { # Zero out all elements of the array. |
| 417 | 41 for i in $( seq $NUMSLOTS ) |
| 418 | 42 do |
| 419 | 43 Slots[$i]=0 |
| 420 | 44 done |
| 421 | 45 |
| 422 | 46 echo # Blank line at beginning of run. |
| 423 | 47 } |
| 424 | 48 |
| 425 | 49 |
| 426 | 50 Show_Slots () { |
| 427 | 51 echo; echo |
| 428 | 52 echo -n " " |
| 429 | 53 for i in $( seq $NUMSLOTS ) # Pretty-print array elements. |
| 430 | 54 do |
| 431 | 55 printf "%3d" ${Slots[$i]} # Allot three spaces per result. |
| 432 | 56 done |
| 433 | 57 |
| 434 | 58 echo # Row of slots: |
| 435 | 59 echo " |__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|" |
| 436 | 60 echo " ||" |
| 437 | 61 echo # Note that if the count within any particular slot exceeds 99, |
| 438 | 62 #+ it messes up the display. |
| 439 | 63 # Running only(!) 500 passes usually avoids this. |
| 440 | 64 } |
| 441 | 65 |
| 442 | 66 |
| 443 | 67 Move () { # Move one unit right / left, or stay put. |
| 444 | 68 Move=$RANDOM # How random is $RANDOM? Well, let's see ... |
| 445 | 69 let "Move %= RANGE" # Normalize into range of 0 - 2. |
| 446 | 70 case "$Move" in |
| 447 | 71 0 ) ;; # Do nothing, i.e., stay in place. |
| 448 | 72 1 ) ((POS--));; # Left. |
| 449 | 73 2 ) ((POS++));; # Right. |
| 450 | 74 * ) echo -n "Error ";; # Anomaly! (Should never occur.) |
| 451 | 75 esac |
| 452 | 76 } |
| 453 | 77 |
| 454 | 78 |
| 455 | 79 Play () { # Single pass (inner loop). |
| 456 | 80 i=0 |
| 457 | 81 while [ "$i" -lt "$ROWS" ] # One event per row. |
| 458 | 82 do |
| 459 | 83 Move |
| 460 | 84 ((i++)); |
| 461 | 85 done |
| 462 | 86 |
| 463 | 87 SHIFT=11 # Why 11, and not 10? |
| 464 | 88 let "POS += $SHIFT" # Shift "zero position" to center. |
| 465 | 89 (( Slots[$POS]++ )) # DEBUG: echo $POS |
| 466 | 90 |
| 467 | 91 # echo -n "$POS " |
| 468 | 92 |
| 469 | 93 } |
| 470 | 94 |
| 471 | 95 |
| 472 | 96 Run () { # Outer loop. |
| 473 | 97 p=0 |
| 474 | 98 while [ "$p" -lt "$PASSES" ] |
| 475 | 99 do |
| 476 | 100 Play |
| 477 | 101 (( p++ )) |
| 478 | 102 POS=0 # Reset to zero. Why? |
| 479 | 103 done |
| 480 | 104 } |
| 481 | 105 |
| 482 | 106 |
| 483 | 107 # -------------- |
| 484 | 108 # main () |
| 485 | 109 Initialize_Slots |
| 486 | 110 Run |
| 487 | 111 Show_Slots |
| 488 | 112 # -------------- |
| 489 | 113 |
| 490 | 114 exit $? |
| 491 | 115 |
| 492 | 116 # Exercises: |
| 493 | 117 # --------- |
| 494 | 118 # 1) Show the results in a vertical bar graph, or as an alternative, |
| 495 | 119 #+ a scattergram. |
| 496 | 120 # 2) Alter the script to use /dev/urandom instead of $RANDOM. |
| 497 | 121 # Will this make the results more random? |
| 498 | 122 # 3) Provide some sort of "animation" or graphic output |
| 499 | 123 # for each marble played.</PRE |
| 500 | ></TD |
| 501 | ></TR |
| 502 | ></TABLE |
| 503 | ><HR></DIV |
| 504 | ><P |
| 505 | > <SPAN |
| 506 | CLASS="emphasis" |
| 507 | ><I |
| 508 | CLASS="EMPHASIS" |
| 509 | >Jipe</I |
| 510 | ></SPAN |
| 511 | > points out a set of techniques for |
| 512 | generating random numbers within a range. |
| 513 | |
| 514 | <TABLE |
| 515 | BORDER="0" |
| 516 | BGCOLOR="#E0E0E0" |
| 517 | WIDTH="100%" |
| 518 | ><TR |
| 519 | ><TD |
| 520 | ><PRE |
| 521 | CLASS="PROGRAMLISTING" |
| 522 | > 1 # Generate random number between 6 and 30. |
| 523 | 2 rnumber=$((RANDOM%25+6)) |
| 524 | 3 |
| 525 | 4 # Generate random number in the same 6 - 30 range, |
| 526 | 5 #+ but the number must be evenly divisible by 3. |
| 527 | 6 rnumber=$(((RANDOM%30/3+1)*3)) |
| 528 | 7 |
| 529 | 8 # Note that this will not work all the time. |
| 530 | 9 # It fails if $RANDOM%30 returns 0. |
| 531 | 10 |
| 532 | 11 # Frank Wang suggests the following alternative: |
| 533 | 12 rnumber=$(( RANDOM%27/3*3+6 ))</PRE |
| 534 | ></TD |
| 535 | ></TR |
| 536 | ></TABLE |
| 537 | > |
| 538 | </P |
| 539 | ><P |
| 540 | > <SPAN |
| 541 | CLASS="emphasis" |
| 542 | ><I |
| 543 | CLASS="EMPHASIS" |
| 544 | >Bill Gradwohl</I |
| 545 | ></SPAN |
| 546 | > came up with an improved |
| 547 | formula that works for positive numbers. |
| 548 | <TABLE |
| 549 | BORDER="0" |
| 550 | BGCOLOR="#E0E0E0" |
| 551 | WIDTH="100%" |
| 552 | ><TR |
| 553 | ><TD |
| 554 | ><PRE |
| 555 | CLASS="PROGRAMLISTING" |
| 556 | > 1 rnumber=$(((RANDOM%(max-min+divisibleBy))/divisibleBy*divisibleBy+min))</PRE |
| 557 | ></TD |
| 558 | ></TR |
| 559 | ></TABLE |
| 560 | > |
| 561 | </P |
| 562 | ><P |
| 563 | >Here Bill presents a versatile function that returns |
| 564 | a random number between two specified values.</P |
| 565 | ><DIV |
| 566 | CLASS="EXAMPLE" |
| 567 | ><HR><A |
| 568 | NAME="RANDOMBETWEEN" |
| 569 | ></A |
| 570 | ><P |
| 571 | ><B |
| 572 | >Example 9-14. Random between values</B |
| 573 | ></P |
| 574 | ><TABLE |
| 575 | BORDER="0" |
| 576 | BGCOLOR="#E0E0E0" |
| 577 | WIDTH="100%" |
| 578 | ><TR |
| 579 | ><TD |
| 580 | ><PRE |
| 581 | CLASS="PROGRAMLISTING" |
| 582 | > 1 #!/bin/bash |
| 583 | 2 # random-between.sh |
| 584 | 3 # Random number between two specified values. |
| 585 | 4 # Script by Bill Gradwohl, with minor modifications by the document author. |
| 586 | 5 # Corrections in lines 187 and 189 by Anthony Le Clezio. |
| 587 | 6 # Used with permission. |
| 588 | 7 |
| 589 | 8 |
| 590 | 9 randomBetween() { |
| 591 | 10 # Generates a positive or negative random number |
| 592 | 11 #+ between $min and $max |
| 593 | 12 #+ and divisible by $divisibleBy. |
| 594 | 13 # Gives a "reasonably random" distribution of return values. |
| 595 | 14 # |
| 596 | 15 # Bill Gradwohl - Oct 1, 2003 |
| 597 | 16 |
| 598 | 17 syntax() { |
| 599 | 18 # Function embedded within function. |
| 600 | 19 echo |
| 601 | 20 echo "Syntax: randomBetween [min] [max] [multiple]" |
| 602 | 21 echo |
| 603 | 22 echo -n "Expects up to 3 passed parameters, " |
| 604 | 23 echo "but all are completely optional." |
| 605 | 24 echo "min is the minimum value" |
| 606 | 25 echo "max is the maximum value" |
| 607 | 26 echo -n "multiple specifies that the answer must be " |
| 608 | 27 echo "a multiple of this value." |
| 609 | 28 echo " i.e. answer must be evenly divisible by this number." |
| 610 | 29 echo |
| 611 | 30 echo "If any value is missing, defaults area supplied as: 0 32767 1" |
| 612 | 31 echo -n "Successful completion returns 0, " |
| 613 | 32 echo "unsuccessful completion returns" |
| 614 | 33 echo "function syntax and 1." |
| 615 | 34 echo -n "The answer is returned in the global variable " |
| 616 | 35 echo "randomBetweenAnswer" |
| 617 | 36 echo -n "Negative values for any passed parameter are " |
| 618 | 37 echo "handled correctly." |
| 619 | 38 } |
| 620 | 39 |
| 621 | 40 local min=${1:-0} |
| 622 | 41 local max=${2:-32767} |
| 623 | 42 local divisibleBy=${3:-1} |
| 624 | 43 # Default values assigned, in case parameters not passed to function. |
| 625 | 44 |
| 626 | 45 local x |
| 627 | 46 local spread |
| 628 | 47 |
| 629 | 48 # Let's make sure the divisibleBy value is positive. |
| 630 | 49 [ ${divisibleBy} -lt 0 ] && divisibleBy=$((0-divisibleBy)) |
| 631 | 50 |
| 632 | 51 # Sanity check. |
| 633 | 52 if [ $# -gt 3 -o ${divisibleBy} -eq 0 -o ${min} -eq ${max} ]; then |
| 634 | 53 syntax |
| 635 | 54 return 1 |
| 636 | 55 fi |
| 637 | 56 |
| 638 | 57 # See if the min and max are reversed. |
| 639 | 58 if [ ${min} -gt ${max} ]; then |
| 640 | 59 # Swap them. |
| 641 | 60 x=${min} |
| 642 | 61 min=${max} |
| 643 | 62 max=${x} |
| 644 | 63 fi |
| 645 | 64 |
| 646 | 65 # If min is itself not evenly divisible by $divisibleBy, |
| 647 | 66 #+ then fix the min to be within range. |
| 648 | 67 if [ $((min/divisibleBy*divisibleBy)) -ne ${min} ]; then |
| 649 | 68 if [ ${min} -lt 0 ]; then |
| 650 | 69 min=$((min/divisibleBy*divisibleBy)) |
| 651 | 70 else |
| 652 | 71 min=$((((min/divisibleBy)+1)*divisibleBy)) |
| 653 | 72 fi |
| 654 | 73 fi |
| 655 | 74 |
| 656 | 75 # If max is itself not evenly divisible by $divisibleBy, |
| 657 | 76 #+ then fix the max to be within range. |
| 658 | 77 if [ $((max/divisibleBy*divisibleBy)) -ne ${max} ]; then |
| 659 | 78 if [ ${max} -lt 0 ]; then |
| 660 | 79 max=$((((max/divisibleBy)-1)*divisibleBy)) |
| 661 | 80 else |
| 662 | 81 max=$((max/divisibleBy*divisibleBy)) |
| 663 | 82 fi |
| 664 | 83 fi |
| 665 | 84 |
| 666 | 85 # --------------------------------------------------------------------- |
| 667 | 86 # Now, to do the real work. |
| 668 | 87 |
| 669 | 88 # Note that to get a proper distribution for the end points, |
| 670 | 89 #+ the range of random values has to be allowed to go between |
| 671 | 90 #+ 0 and abs(max-min)+divisibleBy, not just abs(max-min)+1. |
| 672 | 91 |
| 673 | 92 # The slight increase will produce the proper distribution for the |
| 674 | 93 #+ end points. |
| 675 | 94 |
| 676 | 95 # Changing the formula to use abs(max-min)+1 will still produce |
| 677 | 96 #+ correct answers, but the randomness of those answers is faulty in |
| 678 | 97 #+ that the number of times the end points ($min and $max) are returned |
| 679 | 98 #+ is considerably lower than when the correct formula is used. |
| 680 | 99 # --------------------------------------------------------------------- |
| 681 | 100 |
| 682 | 101 spread=$((max-min)) |
| 683 | 102 # Omair Eshkenazi points out that this test is unnecessary, |
| 684 | 103 #+ since max and min have already been switched around. |
| 685 | 104 [ ${spread} -lt 0 ] && spread=$((0-spread)) |
| 686 | 105 let spread+=divisibleBy |
| 687 | 106 randomBetweenAnswer=$(((RANDOM%spread)/divisibleBy*divisibleBy+min)) |
| 688 | 107 |
| 689 | 108 return 0 |
| 690 | 109 |
| 691 | 110 # However, Paulo Marcel Coelho Aragao points out that |
| 692 | 111 #+ when $max and $min are not divisible by $divisibleBy, |
| 693 | 112 #+ the formula fails. |
| 694 | 113 # |
| 695 | 114 # He suggests instead the following formula: |
| 696 | 115 # rnumber = $(((RANDOM%(max-min+1)+min)/divisibleBy*divisibleBy)) |
| 697 | 116 |
| 698 | 117 } |
| 699 | 118 |
| 700 | 119 # Let's test the function. |
| 701 | 120 min=-14 |
| 702 | 121 max=20 |
| 703 | 122 divisibleBy=3 |
| 704 | 123 |
| 705 | 124 |
| 706 | 125 # Generate an array of expected answers and check to make sure we get |
| 707 | 126 #+ at least one of each answer if we loop long enough. |
| 708 | 127 |
| 709 | 128 declare -a answer |
| 710 | 129 minimum=${min} |
| 711 | 130 maximum=${max} |
| 712 | 131 if [ $((minimum/divisibleBy*divisibleBy)) -ne ${minimum} ]; then |
| 713 | 132 if [ ${minimum} -lt 0 ]; then |
| 714 | 133 minimum=$((minimum/divisibleBy*divisibleBy)) |
| 715 | 134 else |
| 716 | 135 minimum=$((((minimum/divisibleBy)+1)*divisibleBy)) |
| 717 | 136 fi |
| 718 | 137 fi |
| 719 | 138 |
| 720 | 139 |
| 721 | 140 # If max is itself not evenly divisible by $divisibleBy, |
| 722 | 141 #+ then fix the max to be within range. |
| 723 | 142 |
| 724 | 143 if [ $((maximum/divisibleBy*divisibleBy)) -ne ${maximum} ]; then |
| 725 | 144 if [ ${maximum} -lt 0 ]; then |
| 726 | 145 maximum=$((((maximum/divisibleBy)-1)*divisibleBy)) |
| 727 | 146 else |
| 728 | 147 maximum=$((maximum/divisibleBy*divisibleBy)) |
| 729 | 148 fi |
| 730 | 149 fi |
| 731 | 150 |
| 732 | 151 |
| 733 | 152 # We need to generate only positive array subscripts, |
| 734 | 153 #+ so we need a displacement that that will guarantee |
| 735 | 154 #+ positive results. |
| 736 | 155 |
| 737 | 156 disp=$((0-minimum)) |
| 738 | 157 for ((i=${minimum}; i<=${maximum}; i+=divisibleBy)); do |
| 739 | 158 answer[i+disp]=0 |
| 740 | 159 done |
| 741 | 160 |
| 742 | 161 |
| 743 | 162 # Now loop a large number of times to see what we get. |
| 744 | 163 loopIt=1000 # The script author suggests 100000, |
| 745 | 164 #+ but that takes a good long while. |
| 746 | 165 |
| 747 | 166 for ((i=0; i<${loopIt}; ++i)); do |
| 748 | 167 |
| 749 | 168 # Note that we are specifying min and max in reversed order here to |
| 750 | 169 #+ make the function correct for this case. |
| 751 | 170 |
| 752 | 171 randomBetween ${max} ${min} ${divisibleBy} |
| 753 | 172 |
| 754 | 173 # Report an error if an answer is unexpected. |
| 755 | 174 [ ${randomBetweenAnswer} -lt ${min} -o ${randomBetweenAnswer} -gt ${max} ] \ |
| 756 | 175 && echo MIN or MAX error - ${randomBetweenAnswer}! |
| 757 | 176 [ $((randomBetweenAnswer%${divisibleBy})) -ne 0 ] \ |
| 758 | 177 && echo DIVISIBLE BY error - ${randomBetweenAnswer}! |
| 759 | 178 |
| 760 | 179 # Store the answer away statistically. |
| 761 | 180 answer[randomBetweenAnswer+disp]=$((answer[randomBetweenAnswer+disp]+1)) |
| 762 | 181 done |
| 763 | 182 |
| 764 | 183 |
| 765 | 184 |
| 766 | 185 # Let's check the results |
| 767 | 186 |
| 768 | 187 for ((i=${minimum}; i<=${maximum}; i+=divisibleBy)); do |
| 769 | 188 [ ${answer[i+disp]} -eq 0 ] \ |
| 770 | 189 && echo "We never got an answer of $i." \ |
| 771 | 190 || echo "${i} occurred ${answer[i+disp]} times." |
| 772 | 191 done |
| 773 | 192 |
| 774 | 193 |
| 775 | 194 exit 0</PRE |
| 776 | ></TD |
| 777 | ></TR |
| 778 | ></TABLE |
| 779 | ><HR></DIV |
| 780 | ><P |
| 781 | >Just how random is <TT |
| 782 | CLASS="VARNAME" |
| 783 | >$RANDOM</TT |
| 784 | >? The best |
| 785 | way to test this is to write a script that tracks |
| 786 | the distribution of <SPAN |
| 787 | CLASS="QUOTE" |
| 788 | >"random"</SPAN |
| 789 | > numbers |
| 790 | generated by <TT |
| 791 | CLASS="VARNAME" |
| 792 | >$RANDOM</TT |
| 793 | >. Let's roll a |
| 794 | <TT |
| 795 | CLASS="VARNAME" |
| 796 | >$RANDOM</TT |
| 797 | > die a few times . . .</P |
| 798 | ><DIV |
| 799 | CLASS="EXAMPLE" |
| 800 | ><HR><A |
| 801 | NAME="RANDOMTEST" |
| 802 | ></A |
| 803 | ><P |
| 804 | ><B |
| 805 | >Example 9-15. Rolling a single die with RANDOM</B |
| 806 | ></P |
| 807 | ><TABLE |
| 808 | BORDER="0" |
| 809 | BGCOLOR="#E0E0E0" |
| 810 | WIDTH="100%" |
| 811 | ><TR |
| 812 | ><TD |
| 813 | ><PRE |
| 814 | CLASS="PROGRAMLISTING" |
| 815 | > 1 #!/bin/bash |
| 816 | 2 # How random is RANDOM? |
| 817 | 3 |
| 818 | 4 RANDOM=$$ # Reseed the random number generator using script process ID. |
| 819 | 5 |
| 820 | 6 PIPS=6 # A die has 6 pips. |
| 821 | 7 MAXTHROWS=600 # Increase this if you have nothing better to do with your time. |
| 822 | 8 throw=0 # Number of times the dice have been cast. |
| 823 | 9 |
| 824 | 10 ones=0 # Must initialize counts to zero, |
| 825 | 11 twos=0 #+ since an uninitialized variable is null, NOT zero. |
| 826 | 12 threes=0 |
| 827 | 13 fours=0 |
| 828 | 14 fives=0 |
| 829 | 15 sixes=0 |
| 830 | 16 |
| 831 | 17 print_result () |
| 832 | 18 { |
| 833 | 19 echo |
| 834 | 20 echo "ones = $ones" |
| 835 | 21 echo "twos = $twos" |
| 836 | 22 echo "threes = $threes" |
| 837 | 23 echo "fours = $fours" |
| 838 | 24 echo "fives = $fives" |
| 839 | 25 echo "sixes = $sixes" |
| 840 | 26 echo |
| 841 | 27 } |
| 842 | 28 |
| 843 | 29 update_count() |
| 844 | 30 { |
| 845 | 31 case "$1" in |
| 846 | 32 0) ((ones++));; # Since a die has no "zero", this corresponds to 1. |
| 847 | 33 1) ((twos++));; # And this to 2. |
| 848 | 34 2) ((threes++));; # And so forth. |
| 849 | 35 3) ((fours++));; |
| 850 | 36 4) ((fives++));; |
| 851 | 37 5) ((sixes++));; |
| 852 | 38 esac |
| 853 | 39 } |
| 854 | 40 |
| 855 | 41 echo |
| 856 | 42 |
| 857 | 43 |
| 858 | 44 while [ "$throw" -lt "$MAXTHROWS" ] |
| 859 | 45 do |
| 860 | 46 let "die1 = RANDOM % $PIPS" |
| 861 | 47 update_count $die1 |
| 862 | 48 let "throw += 1" |
| 863 | 49 done |
| 864 | 50 |
| 865 | 51 print_result |
| 866 | 52 |
| 867 | 53 exit $? |
| 868 | 54 |
| 869 | 55 # The scores should distribute evenly, assuming RANDOM is random. |
| 870 | 56 # With $MAXTHROWS at 600, all should cluster around 100, |
| 871 | 57 #+ plus-or-minus 20 or so. |
| 872 | 58 # |
| 873 | 59 # Keep in mind that RANDOM is a ***pseudorandom*** generator, |
| 874 | 60 #+ and not a spectacularly good one at that. |
| 875 | 61 |
| 876 | 62 # Randomness is a deep and complex subject. |
| 877 | 63 # Sufficiently long "random" sequences may exhibit |
| 878 | 64 #+ chaotic and other "non-random" behavior. |
| 879 | 65 |
| 880 | 66 # Exercise (easy): |
| 881 | 67 # --------------- |
| 882 | 68 # Rewrite this script to flip a coin 1000 times. |
| 883 | 69 # Choices are "HEADS" and "TAILS."</PRE |
| 884 | ></TD |
| 885 | ></TR |
| 886 | ></TABLE |
| 887 | ><HR></DIV |
| 888 | ><P |
| 889 | >As we have seen in the last example, it is best to |
| 890 | <I |
| 891 | CLASS="FIRSTTERM" |
| 892 | >reseed</I |
| 893 | > the <TT |
| 894 | CLASS="PARAMETER" |
| 895 | ><I |
| 896 | >RANDOM</I |
| 897 | ></TT |
| 898 | > |
| 899 | generator each time it is invoked. Using the same seed |
| 900 | for <TT |
| 901 | CLASS="PARAMETER" |
| 902 | ><I |
| 903 | >RANDOM</I |
| 904 | ></TT |
| 905 | > repeats the same series |
| 906 | of numbers. |
| 907 | <A |
| 908 | NAME="AEN5857" |
| 909 | HREF="#FTN.AEN5857" |
| 910 | >[2]</A |
| 911 | > |
| 912 | (This mirrors the behavior of the |
| 913 | <TT |
| 914 | CLASS="REPLACEABLE" |
| 915 | ><I |
| 916 | >random()</I |
| 917 | ></TT |
| 918 | > function in |
| 919 | <I |
| 920 | CLASS="FIRSTTERM" |
| 921 | >C</I |
| 922 | >.)</P |
| 923 | ><DIV |
| 924 | CLASS="EXAMPLE" |
| 925 | ><HR><A |
| 926 | NAME="SEEDINGRANDOM" |
| 927 | ></A |
| 928 | ><P |
| 929 | ><B |
| 930 | >Example 9-16. Reseeding RANDOM</B |
| 931 | ></P |
| 932 | ><TABLE |
| 933 | BORDER="0" |
| 934 | BGCOLOR="#E0E0E0" |
| 935 | WIDTH="100%" |
| 936 | ><TR |
| 937 | ><TD |
| 938 | ><PRE |
| 939 | CLASS="PROGRAMLISTING" |
| 940 | > 1 #!/bin/bash |
| 941 | 2 # seeding-random.sh: Seeding the RANDOM variable. |
| 942 | 3 # v 1.1, reldate 09 Feb 2013 |
| 943 | 4 |
| 944 | 5 MAXCOUNT=25 # How many numbers to generate. |
| 945 | 6 SEED= |
| 946 | 7 |
| 947 | 8 random_numbers () |
| 948 | 9 { |
| 949 | 10 local count=0 |
| 950 | 11 local number |
| 951 | 12 |
| 952 | 13 while [ "$count" -lt "$MAXCOUNT" ] |
| 953 | 14 do |
| 954 | 15 number=$RANDOM |
| 955 | 16 echo -n "$number " |
| 956 | 17 let "count++" |
| 957 | 18 done |
| 958 | 19 } |
| 959 | 20 |
| 960 | 21 echo; echo |
| 961 | 22 |
| 962 | 23 SEED=1 |
| 963 | 24 RANDOM=$SEED # Setting RANDOM seeds the random number generator. |
| 964 | 25 echo "Random seed = $SEED" |
| 965 | 26 random_numbers |
| 966 | 27 |
| 967 | 28 |
| 968 | 29 RANDOM=$SEED # Same seed for RANDOM . . . |
| 969 | 30 echo; echo "Again, with same random seed ..." |
| 970 | 31 echo "Random seed = $SEED" |
| 971 | 32 random_numbers # . . . reproduces the exact same number series. |
| 972 | 33 # |
| 973 | 34 # When is it useful to duplicate a "random" series? |
| 974 | 35 |
| 975 | 36 echo; echo |
| 976 | 37 |
| 977 | 38 SEED=2 |
| 978 | 39 RANDOM=$SEED # Trying again, but with a different seed . . . |
| 979 | 40 echo "Random seed = $SEED" |
| 980 | 41 random_numbers # . . . gives a different number series. |
| 981 | 42 |
| 982 | 43 echo; echo |
| 983 | 44 |
| 984 | 45 # RANDOM=$$ seeds RANDOM from process id of script. |
| 985 | 46 # It is also possible to seed RANDOM from 'time' or 'date' commands. |
| 986 | 47 |
| 987 | 48 # Getting fancy... |
| 988 | 49 SEED=$(head -1 /dev/urandom | od -N 1 | awk '{ print $2 }'| sed s/^0*//) |
| 989 | 50 # Pseudo-random output fetched |
| 990 | 51 #+ from /dev/urandom (system pseudo-random device-file), |
| 991 | 52 #+ then converted to line of printable (octal) numbers by "od", |
| 992 | 53 #+ then "awk" retrieves just one number for SEED, |
| 993 | 54 #+ finally "sed" removes any leading zeros. |
| 994 | 55 RANDOM=$SEED |
| 995 | 56 echo "Random seed = $SEED" |
| 996 | 57 random_numbers |
| 997 | 58 |
| 998 | 59 echo; echo |
| 999 | 60 |
| 1000 | 61 exit 0</PRE |
| 1001 | ></TD |
| 1002 | ></TR |
| 1003 | ></TABLE |
| 1004 | ><HR></DIV |
| 1005 | ><P |
| 1006 | ><A |
| 1007 | NAME="URANDOMREF" |
| 1008 | ></A |
| 1009 | ></P |
| 1010 | ><DIV |
| 1011 | CLASS="NOTE" |
| 1012 | ><TABLE |
| 1013 | CLASS="NOTE" |
| 1014 | WIDTH="100%" |
| 1015 | BORDER="0" |
| 1016 | ><TR |
| 1017 | ><TD |
| 1018 | WIDTH="25" |
| 1019 | ALIGN="CENTER" |
| 1020 | VALIGN="TOP" |
| 1021 | ><IMG |
| 1022 | SRC="common/note.png" |
| 1023 | HSPACE="5" |
| 1024 | ALT="Note"></TD |
| 1025 | ><TD |
| 1026 | ALIGN="LEFT" |
| 1027 | VALIGN="TOP" |
| 1028 | ><P |
| 1029 | >The <TT |
| 1030 | CLASS="FILENAME" |
| 1031 | >/dev/urandom</TT |
| 1032 | > pseudo-device file |
| 1033 | provides a method of generating much more <SPAN |
| 1034 | CLASS="QUOTE" |
| 1035 | >"random"</SPAN |
| 1036 | > |
| 1037 | pseudorandom numbers than the <TT |
| 1038 | CLASS="VARNAME" |
| 1039 | >$RANDOM</TT |
| 1040 | > |
| 1041 | variable. <TT |
| 1042 | CLASS="USERINPUT" |
| 1043 | ><B |
| 1044 | >dd if=/dev/urandom of=targetfile |
| 1045 | bs=1 count=XX</B |
| 1046 | ></TT |
| 1047 | > creates a file of well-scattered |
| 1048 | pseudorandom numbers. However, assigning these numbers |
| 1049 | to a variable in a script requires a workaround, such |
| 1050 | as filtering through <A |
| 1051 | HREF="extmisc.html#ODREF" |
| 1052 | >od</A |
| 1053 | > |
| 1054 | (as in above example, <A |
| 1055 | HREF="textproc.html#RND" |
| 1056 | >Example 16-14</A |
| 1057 | >, and |
| 1058 | <A |
| 1059 | HREF="contributed-scripts.html#INSERTIONSORT" |
| 1060 | >Example A-36</A |
| 1061 | >), or even piping to |
| 1062 | <A |
| 1063 | HREF="filearchiv.html#MD5SUMREF" |
| 1064 | >md5sum</A |
| 1065 | > (see <A |
| 1066 | HREF="colorizing.html#HORSERACE" |
| 1067 | >Example 36-16</A |
| 1068 | >).</P |
| 1069 | ><P |
| 1070 | ><A |
| 1071 | NAME="AWKRANDOMREF" |
| 1072 | ></A |
| 1073 | ></P |
| 1074 | ><P |
| 1075 | >There are also other ways to generate pseudorandom |
| 1076 | numbers in a script. <B |
| 1077 | CLASS="COMMAND" |
| 1078 | >Awk</B |
| 1079 | > provides a |
| 1080 | convenient means of doing this.</P |
| 1081 | ><DIV |
| 1082 | CLASS="EXAMPLE" |
| 1083 | ><HR><A |
| 1084 | NAME="RANDOM2" |
| 1085 | ></A |
| 1086 | ><P |
| 1087 | ><B |
| 1088 | >Example 9-17. Pseudorandom numbers, using <A |
| 1089 | HREF="awk.html#AWKREF" |
| 1090 | >awk</A |
| 1091 | ></B |
| 1092 | ></P |
| 1093 | ><TABLE |
| 1094 | BORDER="0" |
| 1095 | BGCOLOR="#E0E0E0" |
| 1096 | WIDTH="100%" |
| 1097 | ><TR |
| 1098 | ><TD |
| 1099 | ><PRE |
| 1100 | CLASS="PROGRAMLISTING" |
| 1101 | > 1 #!/bin/bash |
| 1102 | 2 # random2.sh: Returns a pseudorandom number in the range 0 - 1, |
| 1103 | 3 #+ to 6 decimal places. For example: 0.822725 |
| 1104 | 4 # Uses the awk rand() function. |
| 1105 | 5 |
| 1106 | 6 AWKSCRIPT=' { srand(); print rand() } ' |
| 1107 | 7 # Command(s)/parameters passed to awk |
| 1108 | 8 # Note that srand() reseeds awk's random number generator. |
| 1109 | 9 |
| 1110 | 10 |
| 1111 | 11 echo -n "Random number between 0 and 1 = " |
| 1112 | 12 |
| 1113 | 13 echo | awk "$AWKSCRIPT" |
| 1114 | 14 # What happens if you leave out the 'echo'? |
| 1115 | 15 |
| 1116 | 16 exit 0 |
| 1117 | 17 |
| 1118 | 18 |
| 1119 | 19 # Exercises: |
| 1120 | 20 # --------- |
| 1121 | 21 |
| 1122 | 22 # 1) Using a loop construct, print out 10 different random numbers. |
| 1123 | 23 # (Hint: you must reseed the srand() function with a different seed |
| 1124 | 24 #+ in each pass through the loop. What happens if you omit this?) |
| 1125 | 25 |
| 1126 | 26 # 2) Using an integer multiplier as a scaling factor, generate random numbers |
| 1127 | 27 #+ in the range of 10 to 100. |
| 1128 | 28 |
| 1129 | 29 # 3) Same as exercise #2, above, but generate random integers this time.</PRE |
| 1130 | ></TD |
| 1131 | ></TR |
| 1132 | ></TABLE |
| 1133 | ><HR></DIV |
| 1134 | ><P |
| 1135 | >The <A |
| 1136 | HREF="timedate.html#DATEREF" |
| 1137 | >date</A |
| 1138 | > command also lends |
| 1139 | itself to <A |
| 1140 | HREF="timedate.html#DATERANDREF" |
| 1141 | >generating pseudorandom |
| 1142 | integer sequences</A |
| 1143 | >.</P |
| 1144 | ></TD |
| 1145 | ></TR |
| 1146 | ></TABLE |
| 1147 | ></DIV |
| 1148 | ></DIV |
| 1149 | ><H3 |
| 1150 | CLASS="FOOTNOTES" |
| 1151 | >Notes</H3 |
| 1152 | ><TABLE |
| 1153 | BORDER="0" |
| 1154 | CLASS="FOOTNOTES" |
| 1155 | WIDTH="100%" |
| 1156 | ><TR |
| 1157 | ><TD |
| 1158 | ALIGN="LEFT" |
| 1159 | VALIGN="TOP" |
| 1160 | WIDTH="5%" |
| 1161 | ><A |
| 1162 | NAME="FTN.AEN5817" |
| 1163 | HREF="randomvar.html#AEN5817" |
| 1164 | >[1]</A |
| 1165 | ></TD |
| 1166 | ><TD |
| 1167 | ALIGN="LEFT" |
| 1168 | VALIGN="TOP" |
| 1169 | WIDTH="95%" |
| 1170 | ><P |
| 1171 | >True <SPAN |
| 1172 | CLASS="QUOTE" |
| 1173 | >"randomness,"</SPAN |
| 1174 | > insofar as |
| 1175 | it exists at all, can only be found in certain incompletely |
| 1176 | understood natural phenomena, such as radioactive |
| 1177 | decay. Computers only <I |
| 1178 | CLASS="FIRSTTERM" |
| 1179 | >simulate</I |
| 1180 | > |
| 1181 | randomness, and computer-generated sequences of |
| 1182 | <SPAN |
| 1183 | CLASS="QUOTE" |
| 1184 | >"random"</SPAN |
| 1185 | > numbers are therefore referred to as |
| 1186 | <I |
| 1187 | CLASS="FIRSTTERM" |
| 1188 | >pseudorandom</I |
| 1189 | >.</P |
| 1190 | ></TD |
| 1191 | ></TR |
| 1192 | ><TR |
| 1193 | ><TD |
| 1194 | ALIGN="LEFT" |
| 1195 | VALIGN="TOP" |
| 1196 | WIDTH="5%" |
| 1197 | ><A |
| 1198 | NAME="FTN.AEN5857" |
| 1199 | HREF="randomvar.html#AEN5857" |
| 1200 | >[2]</A |
| 1201 | ></TD |
| 1202 | ><TD |
| 1203 | ALIGN="LEFT" |
| 1204 | VALIGN="TOP" |
| 1205 | WIDTH="95%" |
| 1206 | ><P |
| 1207 | >The <I |
| 1208 | CLASS="FIRSTTERM" |
| 1209 | >seed</I |
| 1210 | > of a |
| 1211 | computer-generated pseudorandom number series |
| 1212 | can be considered an identification label. For |
| 1213 | example, think of the pseudorandom series with a |
| 1214 | seed of <SPAN |
| 1215 | CLASS="emphasis" |
| 1216 | ><I |
| 1217 | CLASS="EMPHASIS" |
| 1218 | >23</I |
| 1219 | ></SPAN |
| 1220 | > as <TT |
| 1221 | CLASS="REPLACEABLE" |
| 1222 | ><I |
| 1223 | >Series |
| 1224 | #23</I |
| 1225 | ></TT |
| 1226 | >.</P |
| 1227 | ><P |
| 1228 | >A property of a pseurandom number series is the length of |
| 1229 | the cycle before it starts repeating itself. A good pseurandom |
| 1230 | generator will produce series with very long cycles.</P |
| 1231 | ></TD |
| 1232 | ></TR |
| 1233 | ></TABLE |
| 1234 | ><DIV |
| 1235 | CLASS="NAVFOOTER" |
| 1236 | ><HR |
| 1237 | ALIGN="LEFT" |
| 1238 | WIDTH="100%"><TABLE |
| 1239 | SUMMARY="Footer navigation table" |
| 1240 | WIDTH="100%" |
| 1241 | BORDER="0" |
| 1242 | CELLPADDING="0" |
| 1243 | CELLSPACING="0" |
| 1244 | ><TR |
| 1245 | ><TD |
| 1246 | WIDTH="33%" |
| 1247 | ALIGN="left" |
| 1248 | VALIGN="top" |
| 1249 | ><A |
| 1250 | HREF="declareref.html" |
| 1251 | ACCESSKEY="P" |
| 1252 | >Prev</A |
| 1253 | ></TD |
| 1254 | ><TD |
| 1255 | WIDTH="34%" |
| 1256 | ALIGN="center" |
| 1257 | VALIGN="top" |
| 1258 | ><A |
| 1259 | HREF="index.html" |
| 1260 | ACCESSKEY="H" |
| 1261 | >Home</A |
| 1262 | ></TD |
| 1263 | ><TD |
| 1264 | WIDTH="33%" |
| 1265 | ALIGN="right" |
| 1266 | VALIGN="top" |
| 1267 | ><A |
| 1268 | HREF="manipulatingvars.html" |
| 1269 | ACCESSKEY="N" |
| 1270 | >Next</A |
| 1271 | ></TD |
| 1272 | ></TR |
| 1273 | ><TR |
| 1274 | ><TD |
| 1275 | WIDTH="33%" |
| 1276 | ALIGN="left" |
| 1277 | VALIGN="top" |
| 1278 | >Typing variables: <B |
| 1279 | CLASS="COMMAND" |
| 1280 | >declare</B |
| 1281 | > or |
| 1282 | <B |
| 1283 | CLASS="COMMAND" |
| 1284 | >typeset</B |
| 1285 | ></TD |
| 1286 | ><TD |
| 1287 | WIDTH="34%" |
| 1288 | ALIGN="center" |
| 1289 | VALIGN="top" |
| 1290 | ><A |
| 1291 | HREF="variables2.html" |
| 1292 | ACCESSKEY="U" |
| 1293 | >Up</A |
| 1294 | ></TD |
| 1295 | ><TD |
| 1296 | WIDTH="33%" |
| 1297 | ALIGN="right" |
| 1298 | VALIGN="top" |
| 1299 | >Manipulating Variables</TD |
| 1300 | ></TR |
| 1301 | ></TABLE |
| 1302 | ></DIV |
| 1303 | ></BODY |
| 1304 | ></HTML |
| 1305 | > |