Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.
6587 Discussions

Problem with random number generator ?

New Contributor I

This isnt a BIG problem, but it might trip someone up if they are not aware of it.

When I call RANDOM_NUMBER(XX) where xx is real(8),

the first value is always a very small number, typically under 1.D-4.

So the first value obtained is not really a random number.

Of course can use RANDOM SEED to get around this, but I thought

it was supposed to use the time and date by default.

at any rate, the first number should be as random as all the following ones, right ?

Has this been addressed before ?

0 Kudos
10 Replies
New Contributor I

Here is a routine you can run to test what I am saying.

I also noticed you get the same results no matter what time of day it is.

I thought the date and time were supposed to initialize the seed ?

Obviously, the time of the day HAS NO EFFECT.

Black Belt

This question is appropriate for the Fortran compiler forum. Nothing in it pertains to MKL, and note that MKL has its own independent set of routines for random number generation.

It is advisable to consult the documentation of the Fortran intrinsic subroutine RANDOM_NUMBER rather instead of guessing what it does. Details of the RNG are implementation-dependent. The Intel documentation for the 2019 compiler says:

If RANDOM_SEED is not used, the processor sets the seed for RANDOM_NUMBER to a processor-dependent value.

As you discovered, the RNG used in Intel Fortran has a default seed array whose values are always the same.  Other Fortran compilers may do this differently. The following program can provide information regarding the RNG for the specific compiler in use.

program trand
  implicit none
  integer :: sz
  integer, allocatable :: s(:)
  real :: y
  call random_seed(size=sz)
  print *,'Size = ',sz                 ! size of seed array
  call random_seed(get=s)              ! retrieve seeds
  print '(1x,A,/,(5I15))','Get: ',s
  call random_number(y)                ! get 1 random number
  print *,'y = ',y
end program


New Contributor I

I could not find the location of a description for RANDOM_NUMBER.

or RANDOM SEED either.

That's why I posted this forum item.

Furthermore, the routine is GIVEN as an MKL routine.

Is that not correct ?

If they talk about it, I would like to know where - I could not find it.

If you call RANDOM_NUMBER, you will see what I meant by the tiny value

you get back the first time.

Anyway, I will play with your example.

Apparently, there is no way to use the date an time for the random seed,

contradicting the description that we get.

shouldnt it reflect the actual behavior ?

Just for the sake of being correct, anyway.

New Contributor I

In your code example, I dont see where we can set the value of SZ.

New Contributor I

The write-up of this routine is really SLOPPY.

It returns two values of SEED, no matter what I do.

Is it supposed to return a random number for each value of SEED?

It does not do that.

Furthermore, I get the same eentsy-teensy value for the first random number returned

that I got yesterday. Is there an INTEL person I can consult this with ?

At least the write-up should be accurate.

Black Belt

The size of the SEED array is implementation dependent. For Ifort, it is 2; for Gfortran, it is 33. You have to regard the whole array as the seed, and not attempt to split off one element. In the test program that I provided, the first call to RANDOM_SEED is for the purpose of finding the size of the array. That size is used to allocate the array, and then a second call is made to obtain the RNG's default seed array.

If you wish to use the date, time, etc.,  to seed the RNG, you have to use that information, with any magic sauce that you want to mix in, to generate the required number of integers to fill the seed array.

New Contributor I

I played around with this -

apparently you are supposed to have a starting value of SEED for each random number

you want back. If you want 10 values of random numbers, the SEED array should have a length of 10.

So, you would input 10 values of SEED. But that does not address the issue of TIME and DATE initializing

the SEED. There is only value for time and date, so how would that give 10 values of SEED?

I think most of you will agree, that if there is a write-up, it should reflect the

actual behavior of the routine.

Black Belt

Your speculations in #8 are incorrect. To see that, simply change the line

real :: y

in #3 to 

real :: y(7)

and run the program.

New Contributor I
I did some more testing. I was under the impression that if you start off with a given value of SEED, that should dictate the sequence of random numbers I would get. The purpose is to be able to repeat the same sequence for debugging purposes. In my code, I put in the same value of ISEED for 10 items in the array. So the STARTING RANDOM NUMBERS generated should all be the same, correct ? It does give me 10 random numbers for each call, but they are all DIFFERENT. So if I want repeated results, HOW would would I get that ? One thing is not clear, is the TYPE of random number that gets returned. It does not tell you whether it is Real(4) or Real(8). Or is that determined by the type of array that they get put into ?
New Contributor I

apparently, only 2 items in the SEED array have any bearing on the outcome.

I tried inputing different items 3 thru 10 in the example I gave, it returns the same set of random numbers.

so its puzzling to me that they allow any size array, since that has no bearing on the result.

Since only one value of SEED has any effect, you can only have one sequence of random numbers.

If you input zeros for SEED, then you get that tiny little number to start.


That still does not answer the question -

why does the time and date have no effect if you dont use an input seed?

One can of course call the TIME and DATE routines, and generate the starting seed from that.