Fortran variable value changing randomly

Click For Summary

Discussion Overview

The discussion revolves around the unexpected behavior of a variable 'seed' in a Fortran program, specifically why its value changes between two locations in a loop. Participants explore the implications of function parameter passing and the behavior of a random number generator (RNG) in relation to the seed variable.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions why the value of 'seed' differs between two print statements in a loop, despite no apparent reassignment.
  • Another participant suggests checking whether function parameters are passed by reference or by value, noting that 'seed' is modified within the function congmult.
  • A later reply confirms that the modification of 'seed' by the function is indeed affecting its value, indicating a misunderstanding of how the function interacts with the variable.
  • Further contributions clarify that all variables in Fortran are passed by reference, emphasizing the importance of maintaining the seed value between calls to the RNG function.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of parameter passing in Fortran and the implications for the RNG, but there are varying levels of understanding regarding the behavior of the seed variable and its modification.

Contextual Notes

There is a lack of clarity regarding the initial assumptions about the function's behavior and the implications of modifying the seed variable within the RNG function.

cpburris
Gold Member
Messages
37
Reaction score
4
This isn't really a question for which the template is useful..
Can anyone explain to me why the value of the variable 'seed' in location 1 is different from the value in location 2? The only thing happening in between is the defining of the varaible v(1) using the real function. I don't see where in that 'seed' is given a new value. I've tried google searching 'Fortran seed' and such and can't find any answer.
Code:
      JRDM = 83721563
      call baysprep(JRDM)
! -----------------------------------------
      subroutine baysprep(first)
          integer first,seed
          common /tobays/ v(53),oldbays,seed
          save /tobays/
          seed = abs(first)
       if (seed.gt.2147483647) seed = 24287
 
       do i = 1,53
!Location 1
        print*,'seed into do loop iteration=',seed
        v(1) = congmult(seed)
!Location 2
        print*,'seed coming out of do loop iteration=',seed
      end do
!
 real function congmult(ix)
!
       integer a,p,ix,b15,b16,xhi,xalo,leftlo,fhi,k
       data a,b15,b16,p/16807,32768,65536,2147483647/
      ! assigns variables these values
       xhi = ix / b16
       xalo = a*(ix - xhi*b16)
       leftlo = xalo / b16
       fhi = xhi*a + leftlo
       k = fhi / b15
       ix = (((xalo-leftlo*b16) - p) + (fhi-k*b15)*b16) + k
       if (ix.lt.0) ix = ix + p
       congmult = real(ix)/2.147483647e+09
       end
 
Last edited by a moderator:
Physics news on Phys.org
cpburris said:
This isn't really a question for which the template is useful..
Hi cpburris. Please use the template for posts to the homework forums regardless of whether you believe it is "suitable" or not. It's just one of those rules we're picky about :smile:

Check to see if your function parameters are call-by-reference or call-by-value. I see that seed is passed to function congmult which takes takes parameter ix and modifies it, so try printing out its value from inside that function just before it returns. See if the value shown matches that of 'seed coming out of do loop iteration'.
 
gneill said:
Hi cpburris. Please use the template for posts to the homework forums regardless of whether you believe it is "suitable" or not. It's just one of those rules we're picky about :smile:

Check to see if your function parameters are call-by-reference or call-by-value. I see that seed is passed to function congmult which takes takes parameter ix and modifies it, so try printing out its value from inside that function just before it returns. See if the value shown matches that of 'seed coming out of do loop iteration'.

Ah, yes you were correct. The modification of the value by the function was being passed back. Thank you very much.
 
You'e welcome. Glad I could help.
 
gneill said:
Check to see if your function parameters are call-by-reference or call-by-value.
The latter doesn't exist in Fortran. All variables are passed by reference.

@cpburris: Note that this is how this RNG keeps track of where it is in the sequence. You give it an initial seed, but then it modifies it every time a new random number is generated. It is important not to change the value of seed between calls to congmult.
 
DrClaude said:
The latter doesn't exist in Fortran. All variables are passed by reference.
It's something that programmers need to be aware of for whatever language they are using. Discovering the hard way is one path to enlightenment :smile:
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
7K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K