mtprng_rand(3f) - [M_random:MERSENNE TWISTER] Obtain the next 32-bit integer in the pseudo-random sequence
(LICENSE:CUSTOM OPEN)
function mtprng_rand(state) result(r)
type(mtprng_state), intent(inout) :: state
integer(INT32) :: r
Obtain the next 32-bit integer in the pseudo-random sequence
state generator state initialized by mtprng_init(3f) or mtprng_init_array(3f)
r The next 32-bit integer in the pseudo-random sequence
Sample program:
program demo_mtprng_rand
use M_random, only : mtprng_state, mtprng_init, mtprng_rand
use, intrinsic :: iso_fortran_env, only : int32
implicit none
integer(INT32) :: seed
type(mtprng_state) :: state
GET_SEED: block
integer :: count
integer :: count_rate
call system_clock(count, count_rate)
seed = count
endblock GET_SEED
call mtprng_init(seed, state)
! returns a INT32 integer with a range in 0 .. 2^31-1
write(*,*) mtprng_rand(state)
end program demo_mtprng_rand
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(mtprng_state), | intent(inout) | :: | state |
function mtprng_rand(state) result(r) ! ident_11="@(#) M_random mtprng_rand(3f) Obtain the next 32-bit integer in the pseudo-random sequence" ! arguments type(mtprng_state), intent(inout) :: state !return type integer(INT32) :: r ! working storage integer(INT64) :: x ! done x = mtprng_rand64(state) if (x > 2147483647_INT64) then r = x - 4294967296_INT64 else r = x endif end function mtprng_rand