📄 randomvar.html
字号:
62 # Sufficiently long "random" sequences may exhibit 63 #+ chaotic and other "non-random" behavior. 64 65 # Exercise (easy): 66 # --------------- 67 # Rewrite this script to flip a coin 1000 times. 68 # Choices are "HEADS" and "TAILS".</PRE></TD></TR></TABLE><HR></DIV><P>As we have seen in the last example, it is best to <ICLASS="FIRSTTERM">reseed</I> the <TTCLASS="PARAMETER"><I>RANDOM</I></TT> generator each time it is invoked. Using the same seed for <TTCLASS="PARAMETER"><I>RANDOM</I></TT> repeats the same series of numbers. <ANAME="AEN5744"HREF="#FTN.AEN5744">[2]</A> (This mirrors the behavior of the <TTCLASS="REPLACEABLE"><I>random()</I></TT> function in <ICLASS="FIRSTTERM">C</I>.)</P><DIVCLASS="EXAMPLE"><HR><ANAME="SEEDINGRANDOM"></A><P><B>Example 9-31. Reseeding RANDOM</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # seeding-random.sh: Seeding the RANDOM variable. 3 4 MAXCOUNT=25 # How many numbers to generate. 5 6 random_numbers () 7 { 8 count=0 9 while [ "$count" -lt "$MAXCOUNT" ] 10 do 11 number=$RANDOM 12 echo -n "$number " 13 let "count += 1" 14 done 15 } 16 17 echo; echo 18 19 RANDOM=1 # Setting RANDOM seeds the random number generator. 20 random_numbers 21 22 echo; echo 23 24 RANDOM=1 # Same seed for RANDOM... 25 random_numbers # ...reproduces the exact same number series. 26 # 27 # When is it useful to duplicate a "random" number series? 28 29 echo; echo 30 31 RANDOM=2 # Trying again, but with a different seed... 32 random_numbers # gives a different number series. 33 34 echo; echo 35 36 # RANDOM=$$ seeds RANDOM from process id of script. 37 # It is also possible to seed RANDOM from 'time' or 'date' commands. 38 39 # Getting fancy... 40 SEED=$(head -1 /dev/urandom | od -N 1 | awk '{ print $2 }') 41 # Pseudo-random output fetched 42 #+ from /dev/urandom (system pseudo-random device-file), 43 #+ then converted to line of printable (octal) numbers by "od", 44 #+ finally "awk" retrieves just one number for SEED. 45 RANDOM=$SEED 46 random_numbers 47 48 echo; echo 49 50 exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="URANDOMREF"></A></P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <TTCLASS="FILENAME">/dev/urandom</TT> pseudo-device file provides a method of generating much more <SPANCLASS="QUOTE">"random"</SPAN> pseudorandom numbers than the <TTCLASS="VARNAME">$RANDOM</TT> variable. <TTCLASS="USERINPUT"><B>dd if=/dev/urandom of=targetfile bs=1 count=XX</B></TT> creates a file of well-scattered pseudorandom numbers. However, assigning these numbers to a variable in a script requires a workaround, such as filtering through <AHREF="extmisc.html#ODREF">od</A> (as in above example, <AHREF="textproc.html#RND">Example 15-14</A>, and <AHREF="contributed-scripts.html#INSERTIONSORT">Example A-38</A>), or using <AHREF="extmisc.html#DDREF">dd</A> (see <AHREF="extmisc.html#BLOTOUT">Example 15-58</A>), or even piping to <AHREF="filearchiv.html#MD5SUMREF">md5sum</A> (see <AHREF="colorizing.html#HORSERACE">Example 33-14</A>).</P><P><ANAME="AWKRANDOMREF"></A></P><P>There are also other ways to generate pseudorandom numbers in a script. <BCLASS="COMMAND">Awk</B> provides a convenient means of doing this.</P><DIVCLASS="EXAMPLE"><HR><ANAME="RANDOM2"></A><P><B>Example 9-32. Pseudorandom numbers, using <AHREF="awk.html#AWKREF">awk</A></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # random2.sh: Returns a pseudorandom number in the range 0 - 1. 3 # Uses the awk rand() function. 4 5 AWKSCRIPT=' { srand(); print rand() } ' 6 # Command(s) / parameters passed to awk 7 # Note that srand() reseeds awk's random number generator. 8 9 10 echo -n "Random number between 0 and 1 = " 11 12 echo | awk "$AWKSCRIPT" 13 # What happens if you leave out the 'echo'? 14 15 exit 0 16 17 18 # Exercises: 19 # --------- 20 21 # 1) Using a loop construct, print out 10 different random numbers. 22 # (Hint: you must reseed the "srand()" function with a different seed 23 #+ in each pass through the loop. What happens if you fail to do this?) 24 25 # 2) Using an integer multiplier as a scaling factor, generate random numbers 26 #+ in the range between 10 and 100. 27 28 # 3) Same as exercise #2, above, but generate random integers this time.</PRE></TD></TR></TABLE><HR></DIV><P>The <AHREF="timedate.html#DATEREF">date</A> command also lends itself to <AHREF="timedate.html#DATERANDREF">generating pseudorandom integer sequences</A>.</P></TD></TR></TABLE></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN5704"HREF="randomvar.html#AEN5704">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>True <SPANCLASS="QUOTE">"randomness,"</SPAN> insofar as it exists at all, can only be found in certain incompletely understood natural phenomena, such as radioactive decay. Computers only <ICLASS="FIRSTTERM">simulate</I> randomness, and computer-generated sequences of <SPANCLASS="QUOTE">"random"</SPAN> numbers are therefore referred to as <ICLASS="FIRSTTERM">pseudorandom</I>.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN5744"HREF="randomvar.html#AEN5744">[2]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>The <ICLASS="FIRSTTERM">seed</I> of a computer-generated pseudorandom number series can be considered an identification label. For example, think of the pseudorandom series with a seed of <SPANCLASS="emphasis"><ICLASS="EMPHASIS">23</I></SPAN> as <SPANCLASS="emphasis"><ICLASS="EMPHASIS">series #23</I></SPAN>.</P><P>A property of a pseurandom number series is the length of the cycle before it starts repeating itself. A good pseurandom generator will produce series with very long cycles.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="ivr.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="dblparens.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Indirect References</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="variables2.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">The Double Parentheses Construct</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -