I mean if I have an initial random value as my seed
, then input
usran(seed)
returns a value in (0,1), then how to call the next value
in the current sequence?
As @francescalus and @Vladmir F commented, this function has a side-effect. From Wikipedia:
In computer science, a function or expression is said to have a side
effect if it modifies some state outside its scope or has an
observable interaction with its calling functions or the outside world
besides returning a value. For example, a particular function might
modify a global variable or static variable, modify one of its
arguments, raise an exception, write data to a display or file, read
data from a keyboard or a file, or call other side-effecting
functions. In the presence of side effects, a program's behaviour may
depend on history; that is, the order of evaluation matters.
Understanding and debugging a function with side effects requires
knowledge about the context and its possible histories. A
function or expression without side effects is said to be pure.
In the specific case of your function, besides returning a random number, it changes the value of its argument with ir=abs(mod(da*ir,db)+0.5d0)
. So each time you call it again with the same variable as argument, ir
will be passed with a different value and thus a different random number will be generated.
"Purity" of procedures plays an imporant role in Fortran context because the posibility of side-effects prevents the compiler to do some code optimizations. A subroutine can still be considered pure if its only side-effect is to modify specific arguments.
But there are cases that, by the very nature of the procedure, side-effects can't be avoided and sometimes even are desired. Gathering random numbers is an example, also are doing I/O, stopping the code or configuring a module.