Print

Print


On Fri, 4 Aug 2000, Olivier Fliegans wrote:

> I would like to generate some integer numbers between 1 and Nt.
> Usually I use the following piece of code
> 
>      call random_number(tmp_ran)
>      i=floor(tmp_ran*real(Nt,4)) + 1
> 
> Unfortunately, under the Lahey compiler (5.5-Linux) this gives me
> numbers between 1 and Nt+1, in other words tmp_ran can be equal to 1.
> (which AFAIK is not standard conforming).
> 
> Any suggestion?
> Thanks in advance

The standard (and simplest) way, used in all Monte Carlo programs I know,
is:

i = INT(tmp_ran*Nt) + 1

I don't know if the standard requires the range of tmp_ran to include 1,
but it would be reasonable to be so. Including 0 is dangerous, because
it can be a source of exceptions (it is quite common to sample from
an exponential distribution by taking the log of a random number).
Anyway, it is very easy to convert a random number sampled in the
interval ]0,1] to one in the interval [0,1[ (and viceversa):

tmp_ran = 1.0 - tmp_ran


Alberto


--------------------------------------------------
Alberto Fasso'
Stanford Linear Accelerator Center
Radiation Physics Dept. ms 48
2575 Sand Hill Road
Menlo Park, CA 94025   

Tel. (1 650) 926 4062        Fax: (1 650) 926 3569




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%