1
0
mirror of https://github.com/php/doc-ja.git synced 2026-03-25 23:52:16 +01:00
Files
archived-doc-ja/reference/random/functions/rand.xml
Takuya Aramaki 37275581b5 random関連のドキュメントの更新 (#298)
* mt_rand() はPHP 8.0以降ValueError をスローする (php/doc-en#4660)

* `getrandmax`が小さいのはPHP 7.1までであることを明示 (php/doc-en#4664)

* 誤字訂正

* Mt19937 の重複についての訳を改善

* 「重複したシードが存在します」→「シードが重複します」
2025-05-27 15:37:24 +09:00

190 lines
5.8 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: bf672f5f2402ee78264a130c52defeed37fe44bf Maintainer: hirokawa Status: ready -->
<!-- CREDITS: takagi,mumumu -->
<refentry xml:id="function.rand" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>rand</refname>
<refpurpose>乱数を生成する</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>int</type><methodname>rand</methodname>
<void/>
</methodsynopsis>
<methodsynopsis>
<type>int</type><methodname>rand</methodname>
<methodparam><type>int</type><parameter>min</parameter></methodparam>
<methodparam><type>int</type><parameter>max</parameter></methodparam>
</methodsynopsis>
<simpara>
オプションの引数
<parameter>min</parameter>,<parameter>max</parameter>
を省略してコールした場合、<function>rand</function> は 0 と
<function>getrandmax</function> の間の擬似乱数(整数)を返します。
例えば、5 から 15 まで(両端を含む)の乱数を得たい場合、
<literal>rand(5, 15)</literal> とします。
</simpara>
&caution.cryptographically-insecure;
<note>
<simpara>
PHP 7.1.0 より前のバージョンでは、Windows のような)いくつかのプラットフォームにおいて
<function>getrandmax</function> は 32767 と小さな値となっていました。
32767 より広い範囲にしたい場合、
<parameter>min</parameter> および <parameter>max</parameter> を指定することで、
これより大きな範囲の乱数を生成することができます。
もしくは、
<function>mt_rand</function> をかわりに使用してみてください。
</simpara>
</note>
<note>
<simpara>
PHP 7.1.0 以降、<function>rand</function> は、
<function>mt_rand</function> と同じ乱数生成器を使います。
下位互換性を保持するために、<function>mt_rand</function>&false; を返すのと対照的に、<function>rand</function>
<parameter>max</parameter><parameter>min</parameter>
よりも小さいことを許します。
</simpara>
</note>
</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>getrandmax</function>)。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
<parameter>min</parameter> (あるいは 0)
から <parameter>max</parameter> (あるいは <function>getrandmax</function>、それぞれ端点を含む)
までの間の疑似乱数値を返します。
</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>
これは、特定のシードから生成されるシーケンスが 64bit PHP 7.1 のそれとは異なる可能性があるということです。
</entry>
</row>
<row>
<entry>7.1.0</entry>
<entry>
<function>rand</function> は、<function>mt_rand</function>
<link linkend="migration71.incompatible.rand-srand-aliases">エイリアスになりました。</link>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>rand</function> の例</title>
<programlisting role="php">
<![CDATA[
<?php
echo rand(), "\n";
echo rand(), "\n";
echo rand(5, 15), "\n";
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
7771
22264
11
]]>
</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>) &lt;= <function>getrandmax</function>
でなければいけないということです。この範囲をこえてしまうと、
<function>rand</function> が返す値のランダム性が、
低品質になってしまいます。
</para>
</warning>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>srand</function></member>
<member><function>getrandmax</function></member>
<member><function>mt_rand</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
-->