⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randomvar.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
  62&nbsp;#  Sufficiently long "random" sequences may exhibit  63&nbsp;#+ chaotic and other "non-random" behavior.  64&nbsp;  65&nbsp;# Exercise (easy):  66&nbsp;# ---------------  67&nbsp;# Rewrite this script to flip a coin 1000 times.  68&nbsp;# 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&nbsp;#!/bin/bash   2&nbsp;# seeding-random.sh: Seeding the RANDOM variable.   3&nbsp;   4&nbsp;MAXCOUNT=25       # How many numbers to generate.   5&nbsp;   6&nbsp;random_numbers ()   7&nbsp;{   8&nbsp;count=0   9&nbsp;while [ "$count" -lt "$MAXCOUNT" ]  10&nbsp;do  11&nbsp;  number=$RANDOM  12&nbsp;  echo -n "$number "  13&nbsp;  let "count += 1"  14&nbsp;done    15&nbsp;}  16&nbsp;  17&nbsp;echo; echo  18&nbsp;  19&nbsp;RANDOM=1          # Setting RANDOM seeds the random number generator.  20&nbsp;random_numbers  21&nbsp;  22&nbsp;echo; echo  23&nbsp;  24&nbsp;RANDOM=1          # Same seed for RANDOM...  25&nbsp;random_numbers    # ...reproduces the exact same number series.  26&nbsp;                  #  27&nbsp;                  # When is it useful to duplicate a "random" number series?  28&nbsp;  29&nbsp;echo; echo  30&nbsp;  31&nbsp;RANDOM=2          # Trying again, but with a different seed...  32&nbsp;random_numbers    # gives a different number series.  33&nbsp;  34&nbsp;echo; echo  35&nbsp;  36&nbsp;# RANDOM=$$  seeds RANDOM from process id of script.  37&nbsp;# It is also possible to seed RANDOM from 'time' or 'date' commands.  38&nbsp;  39&nbsp;# Getting fancy...  40&nbsp;SEED=$(head -1 /dev/urandom | od -N 1 | awk '{ print $2 }')  41&nbsp;#  Pseudo-random output fetched  42&nbsp;#+ from /dev/urandom (system pseudo-random device-file),  43&nbsp;#+ then converted to line of printable (octal) numbers by "od",  44&nbsp;#+ finally "awk" retrieves just one number for SEED.  45&nbsp;RANDOM=$SEED  46&nbsp;random_numbers  47&nbsp;  48&nbsp;echo; echo  49&nbsp;  50&nbsp;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&nbsp;#!/bin/bash   2&nbsp;# random2.sh: Returns a pseudorandom number in the range 0 - 1.   3&nbsp;# Uses the awk rand() function.   4&nbsp;   5&nbsp;AWKSCRIPT=' { srand(); print rand() } '   6&nbsp;#            Command(s) / parameters passed to awk   7&nbsp;# Note that srand() reseeds awk's random number generator.   8&nbsp;   9&nbsp;  10&nbsp;echo -n "Random number between 0 and 1 = "  11&nbsp;  12&nbsp;echo | awk "$AWKSCRIPT"  13&nbsp;# What happens if you leave out the 'echo'?  14&nbsp;  15&nbsp;exit 0  16&nbsp;  17&nbsp;  18&nbsp;# Exercises:  19&nbsp;# ---------  20&nbsp;  21&nbsp;# 1) Using a loop construct, print out 10 different random numbers.  22&nbsp;#      (Hint: you must reseed the "srand()" function with a different seed  23&nbsp;#+     in each pass through the loop. What happens if you fail to do this?)  24&nbsp;  25&nbsp;# 2) Using an integer multiplier as a scaling factor, generate random numbers   26&nbsp;#+   in the range between 10 and 100.  27&nbsp;  28&nbsp;# 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 + -