Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fortran code to calculate random numbers by lcg method.
!****************************************************
! 一様乱数計算(by 線形合同法)
! : 0 〜 1 の一様乱数を線形合同法により計算する
!
! date name version
! 2017.04.08 mk-mode.com 1.00 新規作成
!
! Copyright(C) 2017 mk-mode.com All Rights Reserved.
!****************************************************
!
! 線形合同法計算モジュール
module lcg
implicit none
private ! default
public :: SP, DP ! constants
public :: rnd ! function
integer, parameter :: SP = kind(1.0)
integer, parameter :: DP = selected_real_kind(2 * precision(1.0_SP))
integer, parameter :: A = 1103515245
integer, parameter :: C = 12345
integer, parameter :: M = 2147483647 ! 2 ** 31 - 1
integer(8) :: x = 12345 ! 初期値 x_0
contains
function rnd()
real(DP) :: rnd
x = mod(A * x + C, M)
rnd = real(x, DP) / (M - 1)
end function rnd
end module lcg
! 主処理
program rndnum_lcg
use lcg
implicit none
integer :: i
integer :: loop_max = 100
do i = 1 , loop_max
print '(I3, ": ", f10.8)', i, rnd()
end do
end program rndnum_lcg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.