1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Fortranで各種疑似乱数を生成

Posted at

Fortranで各種疑似乱数を簡単に生成できるようにしました。

数ヶ月運用していて、地味に使いやすいので重宝しています。

ソースコードはこちらになります。
https://github.com/kazulagi/plantFEM

場所はsrc/RandomClass/RandomClass.f90 にあります。

文法はrandom % 分布(パラメータ)とシンプル

まずはシンプルな例から。それぞれ100個生成しています。戻り値はreal(real64)型です。

program main
    use RandomClass
    implicit none

    type(Random_) ::random
    integer(int32) :: i

    ! [0:1)の一様乱数
    do i=1, 100
        print *, random%random()
    enddo
    
    ! ガウス分布
    do i=1, 100
        print *, random%gauss(mu=10.0d0,sigma=2.0d0)
    enddo
    
    ! χ二乗分布
    do i=1,100
        print *, random%ChiSquared(K=10.0d0)
    enddo
    
    ! コーシー分布
    do i=1,100
        print *, random%Chauchy(mu=10.0d0,gamma=2.0d0)
    enddo
    
    ! 対数正規分布
    do i=1,100
        print *, random%Lognormal(mu=10.0d0,sigma=2.0d0)
    enddo
    
    ! 逆ガウス分布
    do i=1,100
        print *, random%InverseGauss(mu=10.0d0,lambda=2.0d0)
    enddo
    
end program

それらをヒストグラムにして、IOClassを使い出力することもできます。

program main
    use RandomClass
    use IOClass
    implicit none

    type(Random_) ::random
    type(IO_) :: f
    integer(int32) :: i
    real(real64),allocatable :: histogram(:,:) ! ヒストグラムを入れておく配列
    real(real64) :: list(10000) ! 乱数列を格納しておく配列

    do i=1,10000
        list(i) = random%gauss(mu=10.0d0,sigma=2.0d0)
    enddo

    ! listに入った数値の最小値から最大値までを20分割してヒストグラムにする。
    histogram =  random%histogram(list=list,division=20) 

    ! ファイルを書き込み用として開く。
    call f%open("gauss.txt",'w')

    do i=1,size(histogram,1)
        write(f%fh,*) histogram(i,1),histogram(i,2)
    enddo
    call f%close()
    
    ! 以下同様

    do i=1,10000
        list(i) = random%ChiSquared(K=10.0d0)
    enddo
    
    histogram =  random%histogram(list=list,division=20)

    call f%open("ChiSquared.txt")
    
    do i=1,size(histogram,1)
        write(f%fh,*) histogram(i,1),histogram(i,2)
    enddo

    call f%close()

    do i=1,10000
        list(i) = random%Chauchy(mu=10.0d0,gamma=2.0d0)
    enddo
    
    histogram =  random%histogram(list=list,division=20)

    call f%open("Chauchy.txt")
    
    do i=1,size(histogram,1)
        write(f%fh,*) histogram(i,1),histogram(i,2)
    enddo
    call f%close()


    do i=1,10000
        list(i) = random%Lognormal(mu=10.0d0,sigma=2.0d0)
    enddo
    
    histogram =  random%histogram(list=list,division=20)

    call f%open("Lognormal.txt")
    
    do i=1,size(histogram,1)
        write(f%fh,*) histogram(i,1),histogram(i,2)
    enddo
    call f%close()



    do i=1,10000
        list(i) = random%InverseGauss(mu=10.0d0,lambda=2.0d0)
    enddo
    
    histogram =  random%histogram(list=list,division=20)

    call f%open("InverseGauss.txt")
    
    do i=1,size(histogram,1)
        write(f%fh,*) histogram(i,1),histogram(i,2)
    enddo
    
    call f%close()

end program

おまけ:ホワイトガウスノイズを生成

program main
    use RandomClass
    use IOClass
    implicit none

    type(Random_) :: random
    type(IO_) :: f
    real(real64) :: g_n
    integer(int32) :: i

    ! get gauss noize wave 
    call f%open("random_wave.csv")
    
    do i=1,1000
        g_n = random%Gauss(mu=0.0d0, sigma=1.0d0) 
        call f%write( trim(str(dble(i))) //", "//str(g_n))
    enddo
    call f%close()

end program main

生成されたホワイトガウスノイズ

image.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?