Skip to content

Commit 96368da

Browse files
authored
random: Add examples for the engine constructors (#2197)
1 parent fbd75a1 commit 96368da

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

reference/random/random/engine/mt19937/construct.xml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,13 @@
7979
<programlisting role="php">
8080
<![CDATA[
8181
<?php
82+
// Uses a random 32 Bit seed.
83+
$e = new \Random\Engine\Mt19937();
8284
83-
/* ... */
84-
85+
$r = new \Random\Randomizer($e);
8586
?>
8687
]]>
8788
</programlisting>
88-
&example.outputs.similar;
89-
<screen>
90-
<![CDATA[
91-
...
92-
]]>
93-
</screen>
9489
</example>
9590
</refsect1>
9691

reference/random/random/engine/pcgoneseq128xslrr64/construct.xml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,36 @@
9797
<programlisting role="php">
9898
<![CDATA[
9999
<?php
100+
// Uses a random 128 Bit seed.
101+
$e = new \Random\Engine\PcgOneseq128XslRr64();
100102
101-
/* ... */
103+
$r = new \Random\Randomizer($e);
104+
?>
105+
]]>
106+
</programlisting>
107+
</example>
108+
<example>
109+
<title>Deriving a seed from a &string;</title>
110+
<programlisting role="php">
111+
<![CDATA[
112+
<?php
113+
$string = "My string seed";
114+
115+
// Hash the string with truncated SHA-256 using binary output
116+
// to turn the $string into a 128 Bit seed. Using the same
117+
// string will result in the same sequence of randomness.
118+
$e = new \Random\Engine\PcgOneseq128XslRr64(
119+
substr( hash( 'sha256', $string, binary: true ), 0, 16 )
120+
);
102121
122+
echo bin2hex( $e->generate() ), "\n";
103123
?>
104124
]]>
105125
</programlisting>
106-
&example.outputs.similar;
126+
&example.outputs;
107127
<screen>
108128
<![CDATA[
109-
...
129+
8333ef59315b16d8
110130
]]>
111131
</screen>
112132
</example>

reference/random/random/engine/xoshiro256starstar/construct.xml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,36 @@
103103
<programlisting role="php">
104104
<![CDATA[
105105
<?php
106+
// Uses a random 256 Bit seed.
107+
$e = new \Random\Engine\Xoshiro256StarStar();
106108
107-
/* ... */
109+
$r = new \Random\Randomizer($e);
110+
?>
111+
]]>
112+
</programlisting>
113+
</example>
114+
<example>
115+
<title>Deriving a seed from a &string;</title>
116+
<programlisting role="php">
117+
<![CDATA[
118+
<?php
119+
$string = "My string seed";
120+
121+
// Hash the string with SHA-256 using binary output to turn the
122+
// $string into a 256 Bit seed. Using the same string will result
123+
// in the same sequence of randomness.
124+
$e = new \Random\Engine\Xoshiro256StarStar(
125+
hash( 'sha256', $string, binary: true )
126+
);
108127
128+
echo bin2hex( $e->generate() ), "\n";
109129
?>
110130
]]>
111131
</programlisting>
112-
&example.outputs.similar;
132+
&example.outputs;
113133
<screen>
114134
<![CDATA[
115-
...
135+
6e013453678388c2
116136
]]>
117137
</screen>
118138
</example>

0 commit comments

Comments
 (0)