📄 109.html
字号:
<tt Class="monofont">)</Tt></p>
</fONT></Td>
<td vALIGn="top">
<fonT SIZe="2">
<p>Returns the hyperbolic cosine of <i><tT CLAss="monofont">x</tt></i>
</p>
</font></td>
</tr>
<tr>
<td vaLigN="top">
<fonT size="2">
<P><tt cLASS="monofont">exp(</tt><i><tT CLAss="monofont">x</tt></I>
<TT Class="monofont">)</TT></P>
</Font></td>
<td valign="top">
<font sizE="2">
<p>Returns <tT claSs="monofont">e ** </tt><i><Tt clASS="monofont">x</Tt></i>
</p>
</fONT></Td>
</tr>
<tR>
<TD ValigN="top">
<FONt size="2">
<p><tt class="monofont">fabs(</tt><i><tt clAss="monofont">x</Tt></i>
<tT clasS="monofont">)</tt></p>
</FONT></td>
<td VALIgn="top">
<foNT SIze="2">
<p>Returns the absolute value of <i><TT CLass="monofont">x</tt></i>
</p>
</font></td>
</tr>
<tr>
<td vAliGn="top">
<foNt sizE="2">
<p><tt CLASs="monofont">floor(</tt><i><TT CLass="monofont">x</tT></I>
<TT clasS="monofont">)</TT></P>
</font></td>
<td valign="top">
<font siZe="2">
<p>Returns the floor of <I><tt cLass="monofont">x</tT></i>
</p>
</fONT></Td>
</tr>
<tR>
<TD ValigN="top">
<FONt sizE="2">
<P><TT class="monofont">fmod(</tt><i><tt class="monofont">x</tt></i>
<tt ClaSs="monofont">, </tt><I><tt clAss="monofont">y</tT></I>
<TT clasS="monofont">)</TT></P>
</font></TD>
<TD valiGN="top">
<FOnt size="2">
<p>Returns <i><tt class="monofont">x</tt></i>
<tt ClaSs="monofont"> % </tt><I><tt clAss="monofont">y</tT></I>
</P>
</Font></tD>
</TR>
<Tr>
<td vALIGn="top">
<fonT SIZe="2">
<p><tt class="monofont">frexp(</tt><i><tt class="monofont">x</tT></i>
<tT claSs="monofont">)</tt></p>
</Font></TD>
<TD valiGN="top">
<FOnt siZE="2">
<P>Returns the positive mantissa and exponent of <I><tt clASS="monofont">x</Tt></i>
</p>
</font></td>
</tr>
<tr>
<td valigN="top">
<foNt siZe="2">
<p><tt ClasS="monofont">hypot(</TT><I><tt clASS="monofont">x</Tt></i>
<tt CLASs="monofont">, </tt><i><TT CLass="monofont">y</tt></i>
<tt class="monofont">)</tt></p>
</font></Td>
<tD valIgn="top">
<foNt siZE="2">
<P>Returns the Euclidean distance, <Tt claSS="monofont">sqrt(</TT><i><tt cLASS="monofont">x</tt></i>
<tT CLAss="monofont"> * </tt><i><tt class="monofont">x</tt></i>
<tt claSs="monofont"> + </tT><i><tt Class="monofont">y</Tt></i>
<tT CLAss="monofont"> * </tt><I><TT Class="monofont">y</TT></I>
<Tt claSS="monofont">)</TT></p>
</font></td>
</tr>
<tr>
<td valign="top">
<fOnt Size="2">
<P><tt clAss="monofont">ldexp(</tT><I><TT clasS="monofont">x</TT></I>
<tt clASS="monofont">, </Tt><i><tt CLASs="monofont">i</tt></i>
<tt class="monofont">)</tt></p>
</font></td>
<Td vAligN="top">
<font Size="2">
<P>Returns <I><TT clasS="monofont">x</TT></I>
<tt clASS="monofont"> * (2 ** </Tt><i><tt CLASs="monofont">i</tt></i>
<tt class="monofont">)</tt></p>
</font></td>
</Tr>
<tR>
<td vAlign="top">
<Font SIZE="2">
<p><tt cLASS="monofont">log(</tt><i><tT CLAss="monofont">x</tt></I>
<TT Class="monofont">)</tt></p>
</font></td>
<td valigN="top">
<foNt siZe="2">
<p>Returns the natural logarithm of <i><tT claSS="monofont">x</TT></i>
</p>
</foNT></TD>
</tr>
<tr>
<TD VAlign="top">
<FONT size="2">
<p><tt class="monofont">log10(</tt><i><tt claSs="monofont">x</tT></i>
<tt Class="monofont">)</Tt></p>
</fONT></Td>
<td vALIGn="top">
<fonT SIZe="2">
<p>Returns the base 10 logarithm of <i><tT CLAss="monofont">x</tt></i>
</p>
</font></td>
</tr>
<tr>
<td vaLigN="top">
<fonT size="2">
<P><tt cLASS="monofont">modf(</tt><i><tT CLAss="monofont">x</tt></I>
<TT Class="monofont">)</TT></P>
</Font></td>
<td valign="top">
<font sizE="2">
<p>Returns the fractional and integer parts of <i><Tt clAss="monofont">x</tt></I>
(both have the same sign as <i><tt CLASs="monofont">x</tt></i>
)</P>
</FONt></td>
</tR>
<TR>
<Td valIGN="top">
<Font size="2">
<p><tt class="monofont">pow(</tt><i><tt ClaSs="monofont">x</tt></I>
<tt clAss="monofont">, </tT><I><TT clasS="monofont">y</TT></I>
<tt clASS="monofont">)</Tt></p>
</foNT></TD>
<td valign="top">
<font size="2">
<p>Returns <i><tt ClaSs="monofont">x</tt></I>
<tt clAss="monofont"> ** </tT><I><TT clasS="monofont">y</TT></I>
</p>
</fonT></TD>
</Tr>
<tr>
<tD VALign="top">
<font size="2">
<p><tt class="monofont">sin(</tT><i><tT claSs="monofont">x</tt></i>
<Tt clASS="monofont">)</Tt></p>
</foNT></TD>
<td vaLIGN="top">
<font SIZE="2">
<p>Returns the sine of <i><tt class="monofont">x</tt></i>
</p>
</font></td>
</tR>
<tr>
<Td vaLign="top">
<fOnt sIZE="2">
<P><tt clASS="monofont">sinh(</Tt><i><tt CLASs="monofont">x</tt></i>
<TT CLass="monofont">)</tt></p>
</font></td>
<td valign="top">
<FonT sizE="2">
<p>Returns the hyperbolic sine of <i><tt ClasS="monofont">x</TT></I>
</p>
</fonT></TD>
</Tr>
<tr>
<tD VALign="top">
<fONT Size="2">
<p><tt class="monofont">sqrt(</tt><i><tt clasS="monofont">x</tt></I>
<tt cLass="monofont">)</tT></p>
</foNT></TD>
<td vaLIGN="top">
<font SIZE="2">
<p>Returns the square root of <i><tt CLASs="monofont">x</tt></i>
</p>
</font></td>
</tr>
<tr>
<td valIgn="top">
<Font Size="2">
<p><Tt clASS="monofont">tan(</Tt><i><tt CLASs="monofont">x</tt></i>
<TT CLass="monofont">)</tT></P>
</FOnt></td>
<td valign="top">
<font size="2">
<P>Returns the tangent of <i><tT claSs="monofont">x</tt></i>
</P>
</fonT></TD>
</Tr>
<tr>
<tD VALign="top">
<fONT Size="2">
<p><TT CLass="monofont">tanh(</tt><i><tt class="monofont">x</tt></i>
<tt clAss="monofont">)</Tt></p>
</fOnt></td>
<Td vaLIGN="top">
<font SIZE="2">
<p>Returns the hyperbolic tangent of <i><tt CLASs="monofont">x</tt></i>
</P>
</FONt></td>
</tr>
</colgroup>
</table></p>
<p>The following constants are defined:</P>
<p><tAble BordeR="1" celLSPAcing="0" CELLpaddING="1" Width="100%">
<COLGroup span="2">
<tr>
<th valign="top">
<foNt sIze="2">
<p><B>Constant</b></p>
</foNt></th>
<TH VAlign="top">
<FONT size="2">
<P><B>Description</B></P>
</font></TH>
</TR>
<tr>
<td valign="top">
<font size="2">
<p><tT clAss="monofont">pi</tT></p>
</fonT></td>
<tD VALign="top">
<fONT Size="2">
<p>Mathematical constant pi</P>
</FONt></td>
</tR>
<TR>
<Td valign="top">
<font size="2">
<p><tt clAss="monofont">e</Tt></p>
</fOnt></td>
<Td vaLIGN="top">
<font SIZE="2">
<p>Mathematical constant e</p>
</foNT></TD>
</tr>
</coLGROup>
</table></p>
<p>? <b>See Also</b> <a href="109#6.html">cmath</a> (129).</p>
<h4><tT clAss="monofont">random</tT></h4>
<p>The <tt ClasS="monofont">random</TT> module provides a variety of functions for generating pseudo-random numbers as well as functions for randomly generating values according to various distributions on the real numbers. Most of the functions in this module depend on the function <Tt claSS="monofont">random()</TT>, which generates uniformly distributed numbers in the range (<tt clASS="monofont">0.0, 1.0</Tt>) using the standard Wichmann-Hill generator.</p>
<p>The following functions are used to control the state of the underlying random-number generator:</p>
<PRE>
<B>seed([ x ])</b> </pre>
<p>Initializes the random-number generator. If <i><tt class="monofont">x</tt></i>
is omitted or is <tt clAss="monofont">None</Tt>, the system time is used to seed the generator. Otherwise, if <i><tT clasS="monofont">x</tt></i>
is an integer or long integer, its value is used. If <I><TT Class="monofont">x</TT></I>
is not an integer, it must be a hashable object and the value of <Tt claSS="monofont">hash(</TT><i><tt cLASS="monofont">x</tt></i>
<tt class="monofont">)</tt> is used as a seed.</p>
<pre>
<b>getstate()</b> </prE>
<p>Returns an object representing the current state of the generator. This object can later be passed to <tT claSs="monofont">setstate()</tt> to restore the state.</p>
<Pre>
<b>setstate(</B><B><I>state</I></b><b>)</b> </pRE>
<P>Restores the state of the random-number generator from an object returned by <Tt claSS="monofont">getstate()</TT>.</p>
<pre>
<B>jumpahead(</B><B><I>n</i></b><b>)</b> </pre>
<p>Quickly changes the state of the generator to what it would be if <tt class="monofont">random()</tt> were called <i><tt ClaSs="monofont">n</tt></I>
times in a row. <i><tt cLass="monofont">n</TT></I>
must be a non-negative integer.</P>
<p>The following function can be used to generate random integers:</p>
<prE>
<B>randrange(</B><B><i>start</i></b><b>,</B> <B><I>stop</I></b> <b>[,</b> <b><I>step</I></B><B>])</b> </pre>
<p>Returns a random integer in <tt class="monofont">range(</tt><i><tt claSs="monofont">start</tT></i>
<tt Class="monofont">,</Tt><i><tT CLAss="monofont">stop</tt></I>
<TT Class="monofont">,</TT><I><Tt claSS="monofont">step</TT></i>
<tt class="monofont">)</tt>. Doesn抰 include the endpoint.</p>
<p>The following functions can be used to randomly manipulate sequences:</p>
<pre>
<b>choice(</b><b><i>seq</I></b><b>)</B> </pre>
<P>Returns a random element from the nonempty sequence <i><tt cLass="monofont">seq</TT></I>
.</P>
<pre>
<b>shuffle(</B><B><I>x</I></b> <b>[,</b> <b><I>random</I></B><B>])</b> </pre>
<P>Randomly shuffles the items in the list <I><TT class="monofont">x</tt></i>
in place. <i><tt class="monofont">random</tt></i>
is an optional argument that specifies a random-generation function. If supplied, it must be a function that takes no arguments and that returns a floating-point number in the range (<tT clAss="monofont">0.0, 1.0</tT>).</p>
<p>The following functions generate random numbers on real numbers. Parameter names correspond to the names in the distribution抯 standard mathematical equation.</p>
<pRe>
<b>betavariate(</b><B><I>alpha</I></B><b>,</b> <b><i>beta</I></B><B>)</B> </pre>
<p>Returns a value between <TT CLass="monofont">0</tT> and <TT Class="monofont">1</tt> from the Beta distribution. <i><tt class="monofont">alpha</tt></i>
<tt cLasS="monofont"> > -1</tt> and <i><Tt claSs="monofont">beta</tt></I>
<TT Class="monofont"> > -1</TT>.</P>
<Pre>
<b>cunifvariate(</b><B><I>mean</I></B><b>,</b> <b><i>arc</I></B><B>)</B> </pre>
<p>Circular uniform distribution. <i><tt class="monofont">mean</tt></i>
is the mean angle and <i><tt clAss="monofont">arc</Tt></i>
is the range of the distribution, centered around the mean angle. Both of these values must be specified in radians in the range between <tT clasS="monofont">0</tt> and <tT CLAss="monofont">pi</tt>. Returned values are in the range <TT CLass="monofont">(</tT><I><TT clasS="monofont">mean</TT></I>
<tt class="monofont"> - </tt><i><tt class="monofont">arc</tt></i>
<Tt cLass="monofont">/2, </Tt><i><tt ClasS="monofont">mean</TT></I>
<tt clASS="monofont"> + </Tt><i><tt CLASs="monofont">arc</tt></i>
<TT CLass="monofont">/2)</tt>.</p>
<pre>
<b>expovariate(</b><b><i>lambd</i></b><b>)</b> </pre>
<P>Exponential distribution. <i><tT claSs="monofont">lambd</tt></i>
is <Tt clASS="monofont">1.0</Tt> divided by the desired mean. Returns values in the range <tt cLASS="monofont">(0, +8)</tt>.</p>
<pRE>
<B>gamma(</B><b><i>alpha</i></b><B>,</B> <B><I>beta</i></b><b>)</b> </pre>
<p>Gamma distribution. <i><tt class="monofont">alpha</tt></i>
<tT clAss="monofont"> > -1, </tT><i><tt cLass="monofont">beta</TT></I>
<Tt claSS="monofont"> > 0</TT>.</p>
<pre>
<B>gauss(</B><B><I>mu</i></b><b>,</b> <B><I>sigma</I></B><b>)</b> </pre>
<p>Gaussian distribution with mean <i><tt class="monofont">mu</tt></i>
and standard deviation <i><tt ClaSs="monofont">sigma</tt></I>
. Slightly faster than <tt clAss="monofont">normalvariate()</tT>.</P>
<PRe>
<b>lognormvariate(</b><b><I>mu</I></B><B>,</b> <b><i>sigma</i></B><B>)</B> </Pre>
<p>Log normal distribution. Taking the natural logarithm of this distribution results in a normal distribution with mean <i><TT CLass="monofont">mu</tt></i>
, standard deviation <i><tt class="monofont">sigma</tt></i>
.</p>
<prE>
<b>normalvariate(</b><B><i>mu</i></b><B>,</b> <b><i>sigma</i></B><b>)</b> </pRE>
<P>Normal distribution with mean <I><tt clASS="monofont">mu</Tt></i>
and standard deviation <i><tT CLAss="monofont">sigma</tt></I>
.</P>
<PRe>
<b>paretovariate(</b><b><i>alpha</i></b><b>)</b> </pre>
<p>Pareto distribution with shape parameter <i><tt clasS="monofont">alpha</tt></I>
.</p>
<prE>
<b>vonmisesvariate(</b><b><i>mu</I></b><b>,</b> <B><I>kappa</I></B><b>)</b> </prE>
<P>von Mises distribution where <I><Tt claSS="monofont">mu</TT></i>
is the mean angle in radians between <tt cLASS="monofont">0</tt> and <tt class="monofont">2 * pi</tt>, and <i><tt class="monofont">kappa</tT></i>
is a non-negative concentration factor. If <i><Tt clAss="monofont">kappa</tt></I>
is zero, the distribution reduces to a uniform random angle over the range <tt cLASS="monofont">0</tt> to <tt CLASs="monofont">2 * pi</tt>.</p>
<PRE>
<B>weibullvariate(</b><b><i>alpha</i></B><B>,</B> <B><i>beta</i></b><b>)</b> </pre>
<p>Weibull distribution with scale parameter <i><tt class="monofont">alpha</tt></i>
and shape parameter <I><tt ClasS="monofont">beta</tt></i>
.</p>
<H5>Notes</h5>
<ul>
<LI>
<P>The <Tt claSS="monofont">Numeric</TT> extension also provides a number of efficient generators for large samples and creating independent random-number streams.</p>
</li>
<lI>
<P>The functions in this module are not thread-safe. If you抮e generating random numbers in different threads, you should use locking to prevent concurrent access.</P>
</Li>
<li>
<p>The period of the random-number generator (before numbers start repeating) is 6,953,607,871,644. Although this is better than that found in many programming libraries, it抯 not suitable for all applications梕specially those related to cryptography.</P>
</LI>
<Li>
<p>New types of random-number generators can be created by subclassing <tt class="monofont">random.Random</tt> and implementing the <tt class="monofont">random()</tt>, <Tt cLass="monofont">seed()</Tt>, <tt cLass="monofont">getstate()</TT>, <TT clasS="monofont">getstate()</TT>, and <Tt claSS="monofont">jumpahead()</TT> methods. All the other functions in this module are actually internally implemented as methods of <tt clASS="monofont">Random</Tt>. Thus, they could be accessed as methods on an instance of the new random-number generator.</p>
</li>
<li>
<p>Significant modifications were made to this module in Python 2.1. Furthermore, some of its functionality was previously found in the deprecated <tt class="monofont">whrandom</tt> module. Please refer to the online documentation for details of these changes.</p>
</li>
</uL></foNt>
<P><TABLE width="100%" border=0><TR valign="top"><TD><font size=1 color="#C0C0C0"><br></font></TD><TD align=right><font size=1 color="#C0C0C0">Last updated on 3/28/2002<br>Python Essential Reference, Second Edition, © 2002 New Riders Publishing</font></TD></TR></TABLE></P>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="108.html" title="Python Services"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=109" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="109.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="110.html" title="String Handling"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
</TD></TR></TABLE>
<!--EndOfBrowse-->
</TD></TR></TABLE>
<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#990000><tr><td><p align=center><font size=1 face="verdana,arial,helvetica" color=white>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -