Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with random numbers

Status
Not open for further replies.

fishytiger

Technical User
Jan 11, 2005
8
CA
I am trying to produce random numbers by using the FORTRAN commands:

call RANDOM_SEED(DATE_TIME(8))
call RANDOM_NUMBER(HARVEST=X)

but each time i run the program, I get the same set of numbers, any idea how to produce different set of numbers at every time?

Fishyiger
 
Hey!, I just learned the built in fortran random subroutine is pseudorandom,
thanks for the help tough!,

The best approach is to build onew own random generator!

fishy
 
I normally use the following: it is a variant of the one from He moans a lot about the lack of randomness but it appears to be OK on my system
Code:
subroutine random_initialize ( )
  integer date_time(8), i, seed, seed_size
  integer, allocatable :: seed_vec(:)
  real t
!
!  Get the seed size
   call random_seed (size = seed_size)
!
!  Allocate space for the seed
   allocate (seed_vec(seed_size))
!
!  Get the current date and time.
   call date_and_time (values = date_time)
!
!  Construct a seed from the date/time
   seed = 0
   do i = 1, 8
      seed = ieor (seed, date_time(i))
   end do
!
!  Create a seed
   do i = 1, seed_size
      seed_vec(i) = ieor (seed, i)
   end do
!
!  Set the random number seed value.
   call random_seed (size = seed_size, put = seed_vec(1:seed_size))
!
!  Free up the seed space.
   deallocate (seed_vec)
   return
end subroutine random_initialize
!
! Test it
program main
   real t
   integer i
   call random_initialize ()
   do i = 1, 10
      call random_number (harvest = t)
      write (*,*) t
   end do
end program main
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top