how to generate integer random number in fortran 90 in the range [0,5]?

后端 未结 2 1364
天涯浪人
天涯浪人 2020-12-31 19:08

I am kind of new in the fortran proramming. Can anyone please help me out with the solution. i am having a problem of generating integer random number in the range [0,5] in

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 19:26

    What about:

    program rand_test
      use,intrinsic :: ISO_Fortran_env
      real(REAL32)  :: r(5)
      integer       :: i(5)
    
      ! call init_random_seed() would go here
    
      call random_number(r)
    
      ! Uniform distribution requires floor: Thanks to @francescalus 
      i = floor( r*6._REAL32 )
    
      print *, i
    end program
    

提交回复
热议问题