coretran icon indicating copy to clipboard operation
coretran copied to clipboard

Issues about the module "m_random"

Open WangYun1995 opened this issue 3 years ago • 4 comments

Hi,

I found three possible issues when I tested "coretran" with some simple programs.

  1. Outputs of rngInteger are constants For testing the functionalities of rngInteger, I write the code as shown below.
       Program main
          ! Import modules
          Use variableKind, only: r64,i32
          Use m_allocate, only: allocate
          Use m_deallocate, only: deallocate
          Use m_random, only: rngInteger, rngNormal, rngUniform
    
          Implicit None
    
         ! Data dictionary: declare parameters
         Integer(i32), allocatable :: a(:)
         Integer(i32) :: N, I
    
         N = 10
    
         ! Allocate memory
         call allocate(a, N)
    
         call rngInteger(a, 1, 10)
    
         Do i = 1, N
             Write (*,*) a(i)
         End Do
    
        ! Deallocate memory
        call deallocate(a)
    
    End Program
    
    

And the results of this code are as shown in the figure below. Obviously, the outputs are not random numbers. SharedScreenshot

  1. The second program is used to test the functionalities of rngNormal. And the code is shown below.
Program main
  ! Import modules
  Use variableKind, only: r64,i32
  Use m_allocate, only: allocate
  Use m_deallocate, only: deallocate
  Use m_random, only: rngInteger, rngNormal, rngUniform
  use m_array1D, only: arange
  use m_maths, only: mean, std
  
  Implicit None
  
  ! Data dictionary: declare parameters
  Real(r64), allocatable :: a(:)
  Integer(i32) :: N, i
  
  N = 10
  
  ! Allocate memory
  call allocate(a, N)
  
  call rngNormal(a)
  
  Do i = 1, N
    Write (*,*) a(i)
  End Do
  
  ! Deallocate memory
  call deallocate(a)

End Program

However, this program output nothing! To be exact, It seemed trapped in an endless cycle because my machine keeps running until I killed this program.

  1. The third example illustrates the issue about rngUniform. The code is shown below.
Program main
  ! Import modules
  Use variableKind, only: r64,i32
  Use m_allocate, only: allocate
  Use m_deallocate, only: deallocate
  Use m_random, only: rngInteger, rngNormal, rngUniform
  use m_array1D, only: arange
  use m_maths, only: mean, std
  
  Implicit None
  
  ! Data dictionary: declare parameters
  Real(r64), allocatable :: a(:)
  Integer(i32) :: N, i
  
  N = 10
  
  ! Allocate memory
  call allocate(a, N)
  
  call rngUniform(a)
  
  Do i = 1, N
    Write (*,*) a(i)
  End Do
  
  ! Deallocate memory
  call deallocate(a)


End Program

And the output of this program is shown in the figure below. I don't understand why this error appears. SharedScreenshot_1

WangYun1995 avatar Feb 08 '21 10:02 WangYun1995

I just confirmed this, thanks for the report! I'll see what I can come up with!

leonfoks avatar Feb 08 '21 19:02 leonfoks

Okay, it seems I have an issue with my "global prng" and how that is being set. If I use a local instantiation of the Prng class, it works fine for me (see below).

Using a local instance is in my opinion preferred as it gives you access to two different generators, and the seed for reproducibility purposes. The Prng class is also thread safe if you wish to use it in parallel and you can jump the state to ensure that each core (or rank in MPI) is getting a different set of random numbers.

I will work on fixing the other approach using a global prng, so you can either switch to this class approach or wait!

Thanks again!

Program main
    ! Import modules
    Use variableKind, only: r64, i32
    use Prng_Class, only: Prng

    Implicit None

    ! Data dictionary: declare parameters

    real(r64) :: r
    real(r64), dimension(10) :: rA

    integer(i32) :: i
    integer(i32), dimension(10) :: iA

    type(Prng) :: rng

    rng = Prng(big = .true., display = .true.)

    call rng%rngUniform(r, 0.d0, 1.d0)
    write(*,*) r
    call rng%rngUniform(rA, 0.d0, 1.d0)
    write(*,*) rA

    call rng%rngNormal(r)
    write(*,*) r
    call rng%rngNormal(rA)
    write(*,*) rA

    call rng%rngInteger(i, 0, 10)
    write(*,*) i

    call rng%rngInteger(iA, 0, 10)
    write(*,*) iA
End Program

leonfoks avatar Feb 08 '21 19:02 leonfoks

You are welcome. BTW, 'coretran' is so cool!

WangYun1995 avatar Feb 09 '21 11:02 WangYun1995

Thank you!!

leonfoks avatar Feb 09 '21 22:02 leonfoks