Random numbers between -5.0 to +5.0

smp
05-25-2010, 06:37 AM
Hi,

I want to generate random numbers between -0.5 to +0.5.

I am using ran1() function, first to generate random numbers between 0.0 and 1.0.

Then I subtract 0.5 from each of these random numbers and then the result is multiplied by 10.

Is this trick a good one, as far as random numbers will be used for scientific purpose.

If not, please let me know the better one.

Thanks,
smp

davekw7x
05-26-2010, 10:41 AM
Hi,

I want to generate random numbers between -0.5 to +0.5...using ran1()I'm thinking you mean between -5.0 and +5.0, right?

Anyhow...

It's the usual method for getting uniform variates over a certain range, given uniform variates on the interval between zero and one.

random numbers will be used for scientific purpose.
You are asking whether it's a good way. Well, it depends wholly on the properties of the random number generator. The NR version 2 text indicates that ran1 is good for some things (maybe "good enough" or not). The ran2 function is touted as being more appropriate for some things (maybe "good enough" or not). Other functions may be "better" for some things (maybe "good enough" or not). Other functions may be faster (but still "good enough"... or, maybe, not).

So the answer to your question as to whether this is a good way is very simple: It depends.

Regards.

Dave