COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### other method to simulate an exponential

• Email
• Follow

```Hello.
I'm programing a simulation of ruin probabilities in C++.
Is there any other method to simulate an exponential faster than
y=-ln(1-uniform(0,1))/b?
I think that using the log function makes things slower in C++.
Many thanks.
Lourdes

```
 0

See related articles to this posting

```In comp.simulation Lourdes <lourdes.afonso@gmail.com> wrote:
> Hello.
> I'm programing a simulation of ruin probabilities in C++.
> Is there any other method to simulate an exponential faster than
> y=-ln(1-uniform(0,1))/b?
> I think that using the log function makes things slower in C++.

Goodness. How many are you doing? ln 1+-x is pretty fast. On typical
desktops it's 1 pipeline cycle. The 1/b may take a couple, but you can

const double foo  = -1./b;

...

y = foo*ln(1-u());

If you have some old crock machine then make a table of pre-determined
u(0,1) and mean.

I would not actually be guess to think generating the numbers is a tiny
fraction of the overall runtime of your sim. Think of how much time goes in
getting the event queue in order (or inserting events in the right place, or
whatever).

```
 0

```I'm doing a_lot of simulations.
Thanks for the tip.

May I ask you what is faster. pow(a,2) or a*a ?
Best regards.

```
 0

```Lourdes wrote:
> Hello.
> I'm programing a simulation of ruin probabilities in C++.
> Is there any other method to simulate an exponential faster than
> y=-ln(1-uniform(0,1))/b?
> I think that using the log function makes things slower in C++.
> Many thanks.
> Lourdes

The fastest way to generate an exponential variable
is probably Marsaglia & Tsang's "ziggurat" method.
See the Journal Statistical Software, v 5, Issue 8, 2000.
http://www.jstatsoft.org/v05/i08/ziggurat.pdf

```
 0

```In comp.simulation Lourdes <lourdes.afonso@gmail.com> wrote:
> I'm doing a_lot of simulations.
> Thanks for the tip.
> May I ask you what is faster. pow(a,2) or a*a ?
[...]

While a pow(a,2) will eventually execute a*a there is obviously a subroutine
call and some cache invalidation extra.

Perhaps h/w & other speed issues should go over to comp.arch.

```
 0

```In article <dgtbfb\$qgj\$1@chessie.cirr.com>,
russell kym horsell  <kym@otaku.freeshell.org> wrote:
>In comp.simulation Lourdes <lourdes.afonso@gmail.com> wrote:
>> I'm doing a_lot of simulations.
>> Thanks for the tip.
>> May I ask you what is faster. pow(a,2) or a*a ?
>[...]

>While a pow(a,2) will eventually execute a*a there is obviously a subroutine
>call and some cache invalidation extra.

Some compilers will set this up, and some will not.
I have used "standard" compilers which would compute
pow(a,2) as exp(2*ln(a)).

>Perhaps h/w & other speed issues should go over to comp.arch.

--
This address is for information only.  I do not claim that these views
are those of the Statistics Department or of Purdue University.
Herman Rubin, Department of Statistics, Purdue University
hrubin@stat.purdue.edu         Phone: (765)494-6054   FAX: (765)494-0558

```
 0

```In comp.simulation Herman Rubin <hrubin@stat.purdue.edu> wrote:
[...]

Long time no see. ;-)

```
 0

```russell kym horsell wrote:
> In comp.simulation Herman Rubin <hrubin@stat.purdue.edu> wrote:
> [...]
>
>
>
> Long time no see. ;-)

Thank you for the informations.
I'm going to use a*a.

Lourdes

```
 0

```russell kym horsell wrote:

> In comp.simulation Lourdes <lourdes.afonso@gmail.com> wrote:
>> I'm doing a_lot of simulations.
>> Thanks for the tip.
>> May I ask you what is faster. pow(a,2) or a*a ?
> [...]
>
>
>
> While a pow(a,2) will eventually execute a*a there is obviously a
> subroutine call and some cache invalidation extra.
>
>
> Perhaps h/w & other speed issues should go over to comp.arch.

That assumes that his compiler does not optimize pow(a,2) to a*a.  BTW for
anything less than a**16 (a^16) (assuming Russian peasant method) pow(a,z)
is clearly slower (for integer z).  That is because i know how the hardware
can do it.  Translation; for integer exponents (z) less than 16 use methods
other than pow(a,z).

--
JosephKK

```
 0

```>I'm programing a simulation of ruin probabilities in C++.
>Is there any other method to simulate an exponential faster than
>y=-ln(1-uniform(0,1))/b?
>I think that using the log function makes things slower in C++.
>Many thanks.
>Lourdes

(1-uniform(0,1))

use

(uniform(0,1))

Because "1-uniform(0,1)" is just as uniformly distributed as
"uniform(0,1)".

Good luck.

```
 0

```"Wim D" <newscomVERVANGCOMDOORWIM@hotmail.com> wrote in message
news:a4atl1pbd3bndsjcvjt97gsnqshus8bag2@4ax.com...
> >I'm programing a simulation of ruin probabilities in C++.
>>Is there any other method to simulate an exponential faster than
>>y=-ln(1-uniform(0,1))/b?
>>I think that using the log function makes things slower in C++.
>>Many thanks.
>>Lourdes
>
>
> (1-uniform(0,1))
>
> use
>
> (uniform(0,1))
>
> Because "1-uniform(0,1)" is just as uniformly distributed as
> "uniform(0,1)".
>
> Good luck.
>
Unless Uniform(0,1) is capable of generating a zero. -ln(0) is undefined.
(1-Uniform(0,1)) also gives trouble if 1.0 is a possible RV.

```
 0