<span style="font-weight: 400;">Pseudorandom number generators (PRNGs) are an integral part of many applications in statistics, modeling, and simulations. In ArrayFire v3.4, we introduce random number generation enhancements that improve speed, accuracy, storage, and unity among the ArrayFire backends.</span>

<span style="font-weight: 400;">Previously in ArrayFire v3.3, each ArrayFire backend used a different PRNG. In ArrayFire v3.4, each ArrayFire backend is able to select from among 3 different random number generators.</span>
<table>
<tbody>
<tr>
<td colspan="2">ArrayFire v3.3 (platform specific)</td>
<td rowspan="4"></td>
<td colspan="2">ArrayFire v3.4 (all generators on all platforms)</td>
</tr>
<tr>
<td>CUDA</td>
<td>XORWOW</td>
<td rowspan="3">CUDA,
OpenCL,
CPU</td>
<td rowspan="3">Philox (CBRNG),
Threefry (CBRNG),
Mersenne Twister</td>
</tr>
<tr>
<td>OpenCL</td>
<td>Threefry (CBRNG)</td>
</tr>
<tr>
<td>CPU</td>
<td>Mersenne Twister</td>
</tr>
</tbody>
</table>
<span style="font-weight: 400;">As seen above, the XORWOW generator (which was only available for CUDA devices previously) has been replaced by the Philox generator which is available along with Threefry and Mersenne Twister on all ArrayFire supported devices. Philox and Threefry are</span><span style="font-weight: 400;"> <a href="http://www.thesalmons.org/john/random123/papers/random123sc11.pdf">Counter Based Random Number Generators</a> (CBRNGs). CBRNGs have two major advantages over PRNGs:</span>
<ol>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Crush Resistance - CBRNGs pass rigorous and extensive statistical tests while Mersenne Twister and XORWOW are not as accurate.</span></li>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Smaller Storage Space - The amount of storage space required to maintain the state of CBRNGs is minimal compared to Mersenne Twister.</span></li>
</ol>
<span style="font-weight: 400;">For these reasons, ArrayFire v3.4 now includes both the Philox and Threefry CBRNGs. Mersenne Twister will continue to be maintained due to its ubiquity in technical computing.</span>
[custom_headline type="left" level="h3" looks_like="h3"]Usage[/custom_headline]
<span style="font-weight: 400;">Users create a random engine object that contains the state of the engine. These objects are initialized by specifying the random engine type and seed. Then the objects are passed into the <span class="lang:default decode:true crayon-inline">randu</span> and <span class="lang:default decode:true crayon-inline">randn</span> functions which generate the sequence of random numbers.</span>
<pre class="lang:c++ decode:true">unsigned seed;
unsigned elem;
af::randomEngine rp(AF_RANDOM_ENGINE_PHILOX, seed);
af::array R = af::randu(elem, f32, rp);</pre>
Another method to select a random engine algorithm is to set the default random engine type. If needed the desired seed can be specified as well. Subsequent calls to <span class="lang:default decode:true crayon-inline ">randu</span>  and <span class="lang:default decode:true crayon-inline ">randn</span>  will utilize the default random engine. These default random engines contain the state of the engine and can be reset or changed by the user.
<pre class="lang:c++ decode:true ">unsigned seed;
setSeed(seed);
setDefaultRandomEngine(AF_RANDOM_ENGINE_THREEFRY);
//Or AF_RANDOM_ENGINE_DEFAULT, AF_RANDOM_ENGINE_PHILOX, AF_RANDOM_ENGINE_MERSENNE
af::array R = af::randu(elem, f32);//Utilizes threefry PRNG algorithm initialized with 'seed'</pre>
The default random engine type is <span class="lang:default decode:true crayon-inline ">AF_RANDOM_ENGINE_PHILOX</span> because it has the best statistical properties among the available generators. For further details on usage, please refer to the <a href="http://arrayfire.org/docs/group__random__mat.htm">documentation</a>.

[custom_headline type="left" level="h3" looks_like="h3"]Performance Results[/custom_headline]

[caption id="attachment_9312" align="aligncenter" width="600"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/cuda.png"><img class="wp-image-9312 size-full" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/cuda.png" alt="OpenCL Backend Performance" width="600" height="371" /></a> OpenCL Backend Performance[/caption]

[caption id="attachment_9313" align="aligncenter" width="600"]<a href="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/opencl.png"><img class="wp-image-9313 size-full" src="http://sandbox.arrayfire.com/wp-content/uploads/2016/09/opencl.png" alt="CUDA Backend Performance" width="600" height="371" /></a> CUDA Backend Performance[/caption]

[custom_headline type="left" level="h3" looks_like="h3"]Future Plans[/custom_headline]

Future releases of ArrayFire will include the XORWOW and MRG32K3a generators for all platforms.

[custom_headline type="left" level="h3" looks_like="h3"]Download[/custom_headline]

ArrayFire v3.4 can be downloaded from these locations:
<ul>
 	<li><a href="http://sandbox.arrayfire.com/download/">Binaries</a></li>
 	<li><a href="https://github.com/arrayfire">Source code</a></li>
</ul>
[custom_headline type="left" level="h3" looks_like="h3"]Community[/custom_headline]

ArrayFire is continually improving through the addition of new random number generators and statistical functions. We welcome your feedback:
<ul>
 	<li>General discussion forums on the <a href="https://groups.google.com/forum/#!forum/arrayfire-users">ArrayFire Google Group</a></li>
 	<li>Live discussion chat on the <a href="https://gitter.im/arrayfire/arrayfire?utm_source=badge&amp;utm_medium=badge&amp;utm_campaign=pr-badge&amp;utm_content=badge">ArrayFire Gitter</a></li>
 	<li>Issue reports on the <a href="https://github.com/arrayfire/arrayfire/issues">ArrayFire GitHub</a></li>
</ul>
Finally, as you find success with ArrayFire, we invite you to contribute a post to this blog to share with the broader community. Email <a href="mailto:scott@arrayfire.com">scott@arrayfire.com</a> to contribute to this blog.