-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathmt-rand.xml
176 lines (174 loc) · 5.37 KB
/
mt-rand.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 0b6c76516a299395a3703e6df44f8ea4cb4549a8 Maintainer: HonestQiao Status: ready -->
<!-- CREDITS: mowangjuanzi, Luffy -->
<refentry xml:id="function.mt-rand" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>mt_rand</refname>
<refpurpose>通过梅森旋转(Mersenne Twister)随机数生成器生成随机值</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>int</type><methodname>mt_rand</methodname>
<void/>
</methodsynopsis>
<methodsynopsis>
<type>int</type><methodname>mt_rand</methodname>
<methodparam><type>int</type><parameter>min</parameter></methodparam>
<methodparam><type>int</type><parameter>max</parameter></methodparam>
</methodsynopsis>
<simpara>
很多老的 libc
的随机数发生器具有一些不确定和未知的特性而且很慢。<function>mt_rand</function>
函数是旧的 <function>rand</function> 的临时替代。该函数用了<link
xlink:href="&url.mersenne;">梅森旋转</link>中已知的特性作为随机数发生器,它可以产生随机数值的平均速度比
libc 提供的 rand() 快四倍。
</simpara>
<simpara>
如果没有提供可选参数 <parameter>min</parameter> 和
<parameter>max</parameter>,<function>mt_rand</function>
返回 0 到 <function>mt_getrandmax</function>
之间的伪随机数。例如想要 5 到 15(包括 5 和 15)之间的随机数,用
<literal>mt_rand(5, 15)</literal>。
</simpara>
&caution.cryptographically-insecure;
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>min</parameter></term>
<listitem>
<para>
可选的、返回的最小值(默认:0)
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>max</parameter></term>
<listitem>
<para>
可选的、返回的最大值(默认:<function>mt_getrandmax</function>)
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
返回 <parameter>min</parameter>(或者 0)到 <parameter>max</parameter>(或者是到
<function>mt_getrandmax</function>,包含这个值)之间的随机整数,如果
<parameter>max</parameter> 小于 <parameter>min</parameter> 则返回 &false;。
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>7.2.0</entry>
<entry>
<function>rand</function> 已收到模偏差的 <link
linkend="migration72.incompatible.rand-mt_rand-output">错误修复</link>。这意味着使用特定种子生成的序列可能与
64 位机器上的 PHP 7.1.0 不同。
</entry>
</row>
<row>
<entry>7.1.0</entry>
<entry>
<function>rand</function> <link linkend="migration71.incompatible.rand-srand-aliases">成为</link> <function>mt_rand</function> 的别名。
</entry>
</row>
<row>
<entry>7.1.0</entry>
<entry>
<function>mt_rand</function> <link
linkend="migration71.incompatible.fixes-to-mt_rand-algorithm">成为</link>使用梅森旋转(Mersenne
Twister)算法的固定、正确版本。要使用旧行为,请使用 <function>mt_srand</function> 并将
<constant>MT_RAND_PHP</constant> 作为第二个参数。
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>mt_rand</function> 例子</title>
<programlisting role="php">
<![CDATA[
<?php
echo mt_rand(), "\n";
echo mt_rand(), "\n";
echo mt_rand(5, 15), "\n";
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
1604716014
1478613278
6
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<warning>
<para>
<parameter>min</parameter> <parameter>max</parameter> 的范围必须在 <function>getrandmax</function>
范围内。即 (<parameter>max</parameter> - <parameter>min</parameter>) <=
<function>getrandmax</function>。否则,<function>rand</function> 可能会返回质量差的随机数。
</para>
</warning>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>mt_srand</function></member>
<member><function>mt_getrandmax</function></member>
<member><function>random_int</function></member>
<member><function>random_bytes</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->