help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fast random numbers


From: Julien Bect
Subject: Re: Fast random numbers
Date: Mon, 25 Jul 2016 11:09:38 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.1.0

Le 25/07/2016 à 10:53, Doug Stewart a écrit :
On Mon, Jul 25, 2016 at 4:08 AM, Max Görner <address@hidden> wrote:
Thank you for your fast reply. Below you can find a script showing two scenarios. The script also shows that the runtime is not due to memory assignment or writing.

   #!/usr/bin/octave --no-init-file
      n = 4.6E7;
   k = 0.72*n;
   tic(); randperm(n, k); toc() #1.7 seconds
   tic(); 1337 + zeros(1, k); toc() #0.2 seconds
      n = 10000;
   tic(); rand(n); toc() #1.7
   tic(); 1337 + zeros(n); toc() #0.7


This last time looks wrong.

here is what I get

[...snip...]
>>      n = 10000;
>>    tic(); rand(n); toc()
Elapsed time is 1.1658 seconds.
>>  tic(); 1337 + zeros(n); toc()
Elapsed time is 1.56768 seconds.

This is surprising.  I get runtimes more similar to those sent by Max on Octave 4.0.3 (Linux 64 bits) :

>> n = 10000;

>> tic(); rand(n); toc()
Elapsed time is 1.13176 seconds.

>>  tic(); 1337 + zeros(n); toc()
Elapsed time is 0.504124 seconds.

But, returning to Max's original question: this is already *very* fast.  It means that generating random numbers is almost as fast copying a constant in memory (only slower by a factor of approximately 2).

Under the hood, Octave uses the standard state-of-the-art Mersenne Twister RNG.

You won't be able to go much faster than this, unless you can use parallel computing (but then you'll need to switch to another RNG, which allows parallel streams of pseudo-random numbers...).

@++
Julien


reply via email to

[Prev in Thread] Current Thread [Next in Thread]