Software Archive
Read-only legacy content

Random number

Intel_C_Intel
Employee
761 Views
Can anyone tell me how to get a random number that is different each time when the routine is excuted?
0 Kudos
4 Replies
Intel_C_Intel
Employee
761 Views
Call RANDOM_SEED with no arguments at the start of your main program. The initializes the seed of the RANDOM_NUMBER subroutine with a value taken from the RTC, I think. If you don't execute the program back to back with less time between invocations than the granularity of the RTC (1 second?) you will get a different set of values from RANDOM_NUMBER each time the program runs.
0 Kudos
Steven_L_Intel1
Employee
761 Views
James is correct, as it applies to Compaq Fortran, but I will caution you that this behavior (randomizing the seed based on the clock) is not specified by the standard. All the standard says is that a call to RANDOM_SEED with no arguments sets the seed to an implementation-defined value. It could be the same value run to run (and in fact was, in our initial implementation.)

Steve
0 Kudos
Intel_C_Intel
Employee
761 Views
James and Steve:

Thank you very much for your replies. Indeed, my question is how to generate a set of random numbers, say m random numbers, between 1 and N. Here m and N are integers and m <= N.
0 Kudos
Intel_C_Intel
Employee
761 Views
 
module all_my_functions 
   implicit none 
   contains 
      subroutine sub 
         integer m 
         integer N 
         integer, allocatable :: ran_ints(:) 
         real, allocatable :: harvest(:) 
         character(20) fmt 
 
! Get the number of random numbers 
         write(*,'(a)',advance='no') 'How many random numbers do you want:> ' 
         read(*,*) m 
! Get the range of random numbers 
         write(*,'(a)',advance='no') 'What should their maximum value be:> ' 
         read(*,*) N 
! Make the arrays big enough 
         allocate(ran_ints(m),harvest(m)) 
! Create random real array 
         call random_number(harvest) 
! Create random integer array 
         ran_ints = harvest*N+1 ! Needs at least CVF 6.5 to work 
! Write out results 
         write(fmt,'(a,i0,a)') '(',m,'(i0:1x))' 
         write(*,fmt) ran_ints 
         return 
      end subroutine sub 
end module all_my_functions 
 
program ran_test 
   use all_my_functions 
   implicit none 
   character(1) response 
   character(6) :: again = '' 
 
! Put this call once in the main program; you shouldn't call 
! random_seed again in the program 
   call random_seed() 
 
! Loop while the user wants to test the subroutine 
   response_loop: do 
      write(*,'(a)',advance='no') 'Do you want to run the test' 
      write(*,'(a)',advance='no') trim(again)//' [y|n]:> ' 
      read(*,'(a1)') response 
      select case(response) 
         case('y','Y') 
            call sub 
            again = ' again' 
         case('n','N') 
            exit response_loop 
         case default 
            write(*,'(a)') "Your response, '"//response//"', was invalid" 
            write(*,'(a)') "Please enter 'y' or 'n' at the prompt" 
      end select 
   end do response_loop 
end program ran_test 
0 Kudos
Reply