12

I am writing a simulation for 2D random walk. I wrote several version of code, and found that the speed is like this (fastest to slowest): C++, *Mathematica* compiled procedural code, *Mathematica* functional code. However, the `rand()`

in C is not good enough for the experiment, and it is said that *Mathematica* uses cellular automation to generate random numbers, which have better distribution, so I wish to use the latter one.

There are two cases, the first one is just plain`Compile`

, the second one uses the option: `CompilationTarget -> "C"`

, which is about 4 times faster than the first one. But the documentation states something scary (for the second case): the function is converted to C, compiled to DLL and linked back. I am wondering that if the compiled program uses the wolfram random algorithm or the rand() provided by C in those cases.

PS: I am using `RandomInteger`

and `RandomReal`

for random number generation.

3Why don't you use a C++ library, e.g. Boost.Random? It is not generally true that

`rand()`

from the C standard library is not good enough. Some implementations are good, some are not so good. The problem is that the implementation you get depends on the system/compiler you use. If you use Boost.Random or a similar library you'll know which precise implementation you have and you'll even have a choice of several different implementations. – Szabolcs – 2015-07-02T11:58:51.3102That said, your question is valid and interesting, but given your motivation for asking it: I'd just use a C++ library. – Szabolcs – 2015-07-02T11:59:47.463

3What I sometimes do is generate a huge block of random number in Mathematica and send this (as a block) to a compiled Mathematica routine. This is quite often much faster than generating the random numbers one by one in the compiled routine. – Sjoerd C. de Vries – 2015-07-02T12:01:49.270

@Szabolcs The actual reason I wanted to switch is that I need to use the data generated by C++, and plot or analysis it in Mathematica, I have to let the program output a file and let mathematica imports it, it is more complicated in Mathematica. – vapor – 2015-07-02T12:02:49.600

1@Felix You can make C++ functions accessible from Mathematica through LibraryLink. This does take a bit of work, but once you've got used to it it's not so bad. This is now the usual way for me to call C++ code (I hate writing to files using newly invented formats and importing the data back). Of course if it's fast enough in pure Mathematica that's much less trouble. – Szabolcs – 2015-07-02T12:06:11.837

2

Or even use C++11 <random>? http://www.cplusplus.com/reference/random/

– dr.blochwave – 2015-07-02T12:24:36.733@SjoerdC.deVries That's a good idea. May I know more details on passing that block of numbers to the function? I think extracting an element from a huge array may take sometime. – vapor – 2015-07-02T12:36:43.097

I think it is unlikely that whatever

– Oleksandr R. – 2015-07-02T13:00:16.003Mathematicaimplements will be dramatically better or worse (by whatever metric) than Mersenne twister, and I certainly would not rely on some rumored superiority for the correctness of my results! If it is very important, you should check the code for the RNG yourself. Thus, it comes back to @Szabolcs's suggestion: use a suitable C++ RNG library that you know has passed the Diehard and Bigcrush tests. See also: Quality of random numbers@Felix Extracting an element from an

array, i.e. numbers stored sequentially in memory in a flat format, takes the same amount of time regardless of the size of the array. – Szabolcs – 2015-07-02T13:02:37.6501You may also like to note that many modern CPUs have a built-in physical random number generator. It may not be suitable (by itself) for cryptographic purposes because its implementation is not totally open and robust against hijacking, but since it is based on thermal noise, there are good physical reasons to suppose that it will produce good-quality random output. – Oleksandr R. – 2015-07-02T13:13:41.183