From 61fdf3c56aff03a84d52adfe3d13fc1b84cc79c7 Mon Sep 17 00:00:00 2001 From: Wesley Cota Date: Thu, 15 Aug 2024 11:14:57 -0300 Subject: [PATCH] add `int32` and single reals --- src/rndgen.f90 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/rndgen.f90 b/src/rndgen.f90 index 8a2e485..48296e4 100644 --- a/src/rndgen.f90 +++ b/src/rndgen.f90 @@ -54,7 +54,9 @@ module rndgen_mod procedure :: rnd => rnd_rndgen_dp ! generates a random number in the range [0, 1) procedure :: int => int_rndgen_i8 ! generates a random integer number in the range [i1, i2] + procedure :: int_i4 => int_rndgen_i4 procedure :: real => real_rndgen_dp ! generates a random real number in the range [r1, r2) + procedure :: real_sp => real_rndgen_sp ! generates a random real number in the range [r1, r2) procedure :: init => init_rndgen procedure :: reset => reset_rndgen @@ -95,6 +97,15 @@ function int_rndgen_i8(this, i1, i2) result(rnd_number) rnd_number = min(int(this%rnd()*(i2 + 1 - i1)) + i1, i2) ! returns in range [i1, i2] end function + !> Generates a random integer number in the range [i1, i2], int32 + function int_rndgen_i4(this, i1, i2) result(rnd_number) + class(rndgen) :: this + integer(kind=i4), intent(in) :: i1, i2 + integer(kind=i4) :: rnd_number + + rnd_number = int(this%int(int(i1,kind=i8), int(i2,kind=i8)), kind=i4) + end function + !> Generates a random real number in the range [r1, r2), double function real_rndgen_dp(this, r1, r2) result(rnd_number) class(rndgen) :: this @@ -104,6 +115,15 @@ function real_rndgen_dp(this, r1, r2) result(rnd_number) rnd_number = r1 + (r2 - r1)*this%rnd() ! returns in range [r1, r2) end function + !> Generates a random real number in the range [r1, r2), single + function real_rndgen_sp(this, r1, r2) result(rnd_number) + class(rndgen) :: this + real(kind=sp), intent(in) :: r1, r2 + real(kind=sp) :: rnd_number + + rnd_number = real(this%real(real(r1, kind=dp), real(r2, kind=dp)), kind=sp) ! returns in range [r1, r2) + end function + !> Initializes the random number generator subroutine init_rndgen(this, iseed) ! Adapted from by Thomas Vojta