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

about Random_seed()

yjyincj
Beginner
2,208 Views
one argument of RANDOM_SEED( ) is size, which is stimulated as follows.

"must be scalar and of type integer. Set to the number of integers (N) that the processor uses to hold the value of the seed."


I tried the example folowed and found N is always equal to 2. If this is the case, why not just make it clear using 2 instead of N? Another argument put is required to be an integer array of rank one and size greater than or equal to N. I don't see the case where N is not equal to 2.


Thanks for your time.
0 Kudos
9 Replies
Steven_L_Intel1
Employee
2,208 Views
Because the number is implementation-dependent. For portable code, you must ask for the value of N and then allocate your seed array to that size. We might decide to change the size in the future if we use a different algorithm. Other compiler vendors make their own choice of algorithm which may have different seed sizes.
0 Kudos
yjyincj
Beginner
2,208 Views
The follows are an example from the manual. The results are also pasted. Would you explain a little bit? Why does gseed get different results?
program main
implicit none
integer::isize,seed(2),gseed(2),hiseed(2),zseed(2)
real ran_harvest(10)
data seed /123456789,987654321/
data hiseed /-1,-1/
data zseed /0,0/
open(unit=10,file='random.txt')
call random_seed(size=isize)
write(10,*)"size",isize

call random_seed(put=hiseed(1:isize))
call random_seed(get=gseed(1:isize))
write(10,*),'hiseed gseed',hiseed,gseed

call random_seed(put=zseed(1:isize))
call random_seed(get=gseed(1:isize))
write(10,*),'zseed gseed',zseed,gseed

call random_seed(put=seed(1:isize))
call random_seed(get=gseed(1:isize))
call random_number(harvest=ran_harvest)
write(10,*),'seed gseed',seed,gseed
write(10,*),"ran_harvest"
write(10,*),ran_harvest
call random_seed(get=gseed(1:isize))
write(10,*),'gseed after harvest',gseed

close(10)
stop
end program main
Results:
size 2
hiseed gseed -1 -1 171 499
zseed gseed 0 0 2147483562 2147483398
seed gseed 123456789 987654321 123456789 987654321
ran_harvest
0.6099895 0.9807593 0.2936640 0.9100145 0.8464803
0.4358687 2.5444608E-02 0.5457680 0.6483381 0.3045360
gseed after harvest 375533067 1869030476
0 Kudos
Steven_L_Intel1
Employee
2,208 Views
Can you elaborate on your question? What do you expect to see?

The purpose of RANDOM_SEED is to allow you to set the state of the random sequence to a known position. If you PUT the same seed, then ask for random_number, you should get the same value. I have not studied the code in detail, but it would not surprise me if it did not take your input seeds verbatim - it might need to do something to make them fit within the range required by the algorithm.
0 Kudos
yjyincj
Beginner
2,208 Views
Sorry for an un-clear question.
put argument is used to reset the value of the seed. get is used to inquire the current value of the seed. In the example, hiseed, zseed, and seed are put to set the seed, respectively; and gseed is used to get the seed after put everytime. But the results are confusing.
the values of hiseed and zseed are /-1,-1/ and /0,0/, the gseed values gotten by get are /171,499/, and /2147483562,2147483398/, respectively. However, gseed has the same value as seed when seed is put by seed which has the value of /123456789,987654321/. Why?

0 Kudos
Les_Neilson
Valued Contributor II
2,208 Views

Steve,

size 2
hiseed gseed -1 -1 171 499
zseed gseed 0 0 2147483562 2147483398

From the documentation (where PUT sets the current seed and GET returns the current seed)
PUT followed by immediate GET (twice)
I would have expected to see

size 2
hiseed gseed -1 -1 -1 -1
zseed gseed 0 0 0 0

The next lineshows the expected values :
seed gseed 123456789 987654321 123456789 987654321
ie the pairs of numbers are the same.

Les

0 Kudos
yjyincj
Beginner
2,208 Views
Yes. This is exactly my question.
0 Kudos
Steven_L_Intel1
Employee
2,208 Views
I'll look at the code when I get back to the office next week, but my guess is that random_seed "adjusts" the seed value if it is not within the range required by the algorithm. 0 or -1 would certainly be out of range.
0 Kudos
yjyincj
Beginner
2,208 Views
Yes. You are right. I read the document again and found the ranges for the two seeds are [1,2147483562] and [1,2147483398]. If the used assigned seed is out of range, the seed might be chosen using the system clock or other mechanism.
0 Kudos
Steven_L_Intel1
Employee
2,208 Views
I looked at the code. If either seed value is zero, it uses a fixed initial seed. If either seed value is greater than (unsigned) the maximum seed value, the value is reduced by subtraction to be within range. If you pass no arguments to RANDOM_SEED, the seed is initialized based on the system clock. Note that this last behavior is NOT specified by the standard, but it is allowed and seems to be what most people want.
0 Kudos
Reply