draw random number from array
- From: "maxndaisy" <laura@xxxxxxxxxxxx>
- Date: 23 Aug 2006 10:42:51 -0700
Hello,
I think that this is a straight forward question but I'm stumped. In my
program I have created an array based on a Gaussian distribution. From
this array I would like to draw a single number randomly to use in
further calculations. To do this I have written a program that
generates an array of index values with the random number 'seeded'
based on the intrinsic function secnds(x). However, the initial index
value stays constant while the rest vary from run to run (program is
posted below).
I have two questions: 1) is there a better approach to randomly draw a
number from an array?
and 2) why does the initial value remain constant for each run when the
'seed' should be changing?
Thank you for your time,
MaxNDaisy
program test_index
integer, parameter::n=5
integer, dimension(n) :: index_num
real, dimension(n) :: q
real:: finq
q=(/0.3979,0.471,0.5106,0.6677,0.4706/)
write(6,*) q
call random_index (index_num,n)
write(6,*) 'q(2)=', q(2)
finq=q(index_num(2))
write(6,*) 'finq=q(index_num(2))', finq
end program test_index
subroutine random_index(index_num,n)
integer, dimension(n) :: index_num
integer seed,seed1, n, i_index ,j
real x
seed1=7654321
x=0.0
! seed should be set to a large odd integer according to the manual
! secnds(x) gives number of seconds-x elapsed since midnight
! the 2*int(secnds(x)) is always even (int=gives integer) so seed is
always odd
seed=seed1+2*int(secnds(x))
do j=1,n
i_index=(n*ran(seed))+1
index_num(j)=i_index
write(6,*) j, i_index
enddo
write(6,*) 'index_num', index_num
return
end subroutine random_index
.
- Follow-Ups:
- Re: draw random number from array
- From: Dr Ivan D. Reid
- Re: draw random number from array
- From: e p chandler
- Re: draw random number from array
- Prev by Date: Re: Syntax or NAMELIST'ing COMMON blocks?
- Next by Date: Re: double precision g77
- Previous by thread: Program received signal SIGFPE, Arithmetic exception (NAG).
- Next by thread: Re: draw random number from array
- Index(es):
Relevant Pages
|