📄 randomvar.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>$RANDOM: generate random integer</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Variables Revisited"HREF="variables2.html"><LINKREL="PREVIOUS"TITLE="Indirect References"HREF="ivr.html"><LINKREL="NEXT"TITLE="The Double Parentheses Construct"HREF="dblparens.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="ivr.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 9. Variables Revisited</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="dblparens.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="RANDOMVAR"></A>9.6. $RANDOM: generate random integer</H1><P><ANAME="RANDOMVAR01"></A></P><P><TTCLASS="VARNAME">$RANDOM</TT> is an internal Bash <AHREF="functions.html#FUNCTIONREF">function</A> (not a constant) that returns a <ICLASS="FIRSTTERM">pseudorandom</I> <ANAME="AEN5704"HREF="#FTN.AEN5704">[1]</A> integer in the range 0 - 32767. It should <TTCLASS="REPLACEABLE"><I>not</I></TT> be used to generate an encryption key.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX21"></A><P><B>Example 9-26. Generating random numbers</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 # $RANDOM returns a different random integer at each invocation. 4 # Nominal range: 0 - 32767 (signed 16-bit integer). 5 6 MAXCOUNT=10 7 count=1 8 9 echo 10 echo "$MAXCOUNT random numbers:" 11 echo "-----------------" 12 while [ "$count" -le $MAXCOUNT ] # Generate 10 ($MAXCOUNT) random integers. 13 do 14 number=$RANDOM 15 echo $number 16 let "count += 1" # Increment count. 17 done 18 echo "-----------------" 19 20 # If you need a random int within a certain range, use the 'modulo' operator. 21 # This returns the remainder of a division operation. 22 23 RANGE=500 24 25 echo 26 27 number=$RANDOM 28 let "number %= $RANGE" 29 # ^^ 30 echo "Random number less than $RANGE --- $number" 31 32 echo 33 34 35 36 # If you need a random integer greater than a lower bound, 37 #+ then set up a test to discard all numbers below that. 38 39 FLOOR=200 40 41 number=0 #initialize 42 while [ "$number" -le $FLOOR ] 43 do 44 number=$RANDOM 45 done 46 echo "Random number greater than $FLOOR --- $number" 47 echo 48 49 # Let's examine a simple alternative to the above loop, namely 50 # let "number = $RANDOM + $FLOOR" 51 # That would eliminate the while-loop and run faster. 52 # But, there might be a problem with that. What is it? 53 54 55 56 # Combine above two techniques to retrieve random number between two limits. 57 number=0 #initialize 58 while [ "$number" -le $FLOOR ] 59 do 60 number=$RANDOM 61 let "number %= $RANGE" # Scales $number down within $RANGE. 62 done 63 echo "Random number between $FLOOR and $RANGE --- $number" 64 echo 65 66 67 68 # Generate binary choice, that is, "true" or "false" value. 69 BINARY=2 70 T=1 71 number=$RANDOM 72 73 let "number %= $BINARY" 74 # Note that let "number >>= 14" gives a better random distribution 75 #+ (right shifts out everything except last binary digit). 76 if [ "$number" -eq $T ] 77 then 78 echo "TRUE" 79 else 80 echo "FALSE" 81 fi 82 83 echo 84 85 86 # Generate a toss of the dice. 87 SPOTS=6 # Modulo 6 gives range 0 - 5. 88 # Incrementing by 1 gives desired range of 1 - 6. 89 # Thanks, Paulo Marcel Coelho Aragao, for the simplification. 90 die1=0 91 die2=0 92 # Would it be better to just set SPOTS=7 and not add 1? Why or why not? 93 94 # Tosses each die separately, and so gives correct odds. 95 96 let "die1 = $RANDOM % $SPOTS +1" # Roll first one. 97 let "die2 = $RANDOM % $SPOTS +1" # Roll second one. 98 # Which arithmetic operation, above, has greater precedence -- 99 #+ modulo (%) or addition (+)? 100 101 102 let "throw = $die1 + $die2" 103 echo "Throw of the dice = $throw" 104 echo 105 106 107 exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="PICKCARD"></A><P><B>Example 9-27. Picking a random card from a deck</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # pick-card.sh 3 4 # This is an example of choosing random elements of an array. 5 6 7 # Pick a card, any card. 8 9 Suites="Clubs 10 Diamonds 11 Hearts 12 Spades" 13 14 Denominations="2 15 3 16 4 17 5 18 6 19 7 20 8 21 9 22 10 23 Jack 24 Queen 25 King 26 Ace" 27 28 # Note variables spread over multiple lines. 29 30 31 suite=($Suites) # Read into array variable. 32 denomination=($Denominations) 33 34 num_suites=${#suite[*]} # Count how many elements. 35 num_denominations=${#denomination[*]} 36 37 echo -n "${denomination[$((RANDOM%num_denominations))]} of " 38 echo ${suite[$((RANDOM%num_suites))]} 39 40 41 # $bozo sh pick-cards.sh 42 # Jack of Clubs 43 44 45 # Thank you, "jipe," for pointing out this use of $RANDOM. 46 exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="BROWNIANREF"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="BROWNIAN"></A><P><B>Example 9-28. Brownian Motion Simulation</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # brownian.sh 3 # Author: Mendel Cooper 4 # Reldate: 10/26/07 5 # License: GPL3 6 7 # ---------------------------------------------------------------- 8 # This script models Brownian motion: 9 #+ the random wanderings of tiny particles in a fluid, 10 #+ as they are buffeted by random currents and collisions. 11 #+ This is colloquially known as the "Drunkard's Walk." 12 13 # It can also be considered as a stripped-down simulation of a 14 #+ Galton Board, a slanted board with a pattern of pegs, 15 #+ down which rolls a succession of marbles, one at a time. 16 #+ At the bottom is a row of slots or catch basins in which 17 #+ the marbles come to rest at the end of their journey. 18 # Think of it as a kind of bare-bones Pachinko game. 19 # As you see by running the script, 20 #+ most of the marbles cluster around the center slot. 21 #+ This is consistent with the expected binomial distribution. 22 # As a Galton Board simulation, the script 23 #+ disregards such parameters as 24 #+ board tilt-angle, rolling friction of the marbles, 25 #+ angles of impact, and elasticity of the pegs. 26 # How does this affect the accuracy of the simulation? 27 # ---------------------------------------------------------------- 28 29 PASSES=500 # Number of particle interactions / marbles. 30 ROWS=10 # Number of "collisions" (or horiz. peg rows). 31 RANGE=3 # 0 - 2 output range from $RANDOM. 32 POS=0 # Left/right position. 33 34 declare -a Slots # Array holding cumulative results of passes. 35 NUMSLOTS=21 # Number of slots at bottom of board. 36 37 38 Initialize_Slots () { # Zero out all elements of array. 39 for i in $( seq $NUMSLOTS ) 40 do 41 Slots[$i]=0 42 done 43 44 echo # Blank line at beginning of run. 45 } 46 47 48 Show_Slots () { 49 echo -n " " 50 for i in $( seq $NUMSLOTS ) # Pretty-print array elements. 51 do 52 printf "%3d" ${Slots[$i]} # Three spaces per result. 53 done 54 55 echo # Row of slots: 56 echo " |__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|" 57 echo " ^^" 58 echo # Note that if the count within any particular slot exceeds 99, 59 #+ it messes up the display. 60 # Running only(!) 500 passes usually avoids this. 61 } 62 63 64 Move () { # Move one unit right / left, or stay put. 65 Move=$RANDOM # How random is $RANDOM? Well, let's see ... 66 let "Move %= RANGE" # Normalize into range of 0 - 2. 67 case "$Move" in 68 0 ) ;; # Do nothing, i.e., stay in place. 69 1 ) ((POS--));; # Left. 70 2 ) ((POS++));; # Right. 71 * ) echo -n "Error ";; # Anomaly! (Should never occur.) 72 esac
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -