Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29391 Discussions

how to use random generator correctly?

z_wang
Beginner
2,267 Views

Dear all,

I want to do a Monte Carlo simulation using fortran. But I was confused by random numbers in visual fortran.
Q1
I want to get mass random numbers over [0,1] from RAND() . I wonder how many times should I use the RANDOM_SEED () routine in the whole simulation to get eligible random numbers? Should I use it only once before the first using of RAND()? or using it every time before RAND() being called? Should I call RNISD() (an IMSL routine to change the seed number) when needed? Since the random number is generated frequently? I am worrying the quality of random number.
Q2
Another question is about the IMSL routine, DRNNOA(), which generates a standard normal distribution N(0,1)? The question is similar. How many times should I call RNSET(ISEED) in whole simulation? Does RANDOM_SEED() take effect on the DRNNOA(..) similar to RNSET(..)?

Thank you very much.

James

0 Kudos
3 Replies
Steven_L_Intel1
Employee
2,267 Views
RANDOM_SEED has no effect on RAND. It is to be used with RANDOM_NUMBER only. I do not recommend using RAND - RANDOM_NUMBER is a standard intrinsic and, at least in our implementation, is a much better generator.

Typically, you call RANDOM_SEED just once at the beginning of the program. If you want to get the same numbers on each run, then set the seed explicitly. If you want different numbers on each run, pass no arguments to RANDOM_SEED and it will set the seed based on the date/time.

You can then call RANDOM_NUMBER to get your numbers. The argument can be a scalar or an array if you want multiple values at once.

If you want to use the IMSL routines, do not use RANDOM_SEED. You can use the IMSL routine for setting the seed, and again it would typically be called just once. If you call it multiple times, you will reduce the quality of the distribution.
0 Kudos
zkv
Beginner
2,267 Views

I have some problems with rand() function.

1) Whenrand is called without an argument, flag is assumed to be 0 (the next random number in the sequence is selected). But this leads to Access Violation in my program.

2) This code:

USE IFPORT

CALL SEED(RND$TIMESEED)

randval = rand(0)

gives error at compile time: The type of the actual argument differs from the type of the dummy argument. [RND$TIMESEED]

Is this compiler bugs? The version of WFC- 9.1.024

0 Kudos
hansr
Beginner
2,267 Views
Try:

USE IFPORT

ii = RND$TIMESEED

CALL SEED (ii)

randval = rand(0)


Hans
0 Kudos
Reply