- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Can anyone tell me how to get a random number that is different each time when the routine is excuted?
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page