Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,4 +1,5 @@
|
|||
A ''subtractive generator'' calculates a sequence of [[random number generator|random numbers]], where each number is congruent to the subtraction of two previous numbers from the sequence. The formula is
|
||||
A ''subtractive generator'' calculates a sequence of [[random number generator|random numbers]], where each number is congruent to the subtraction of two previous numbers from the sequence. <br>
|
||||
The formula is
|
||||
|
||||
* <math>r_n = r_{(n - i)} - r_{(n - j)} \pmod m</math>
|
||||
|
||||
|
|
|
|||
56
Task/Subtractive-generator/Fortran/subtractive-generator.f
Normal file
56
Task/Subtractive-generator/Fortran/subtractive-generator.f
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
module subgenerator
|
||||
implicit none
|
||||
|
||||
integer, parameter :: modulus = 1000000000
|
||||
integer :: s(0:54), r(0:54)
|
||||
|
||||
contains
|
||||
|
||||
subroutine initgen(seed)
|
||||
integer :: seed
|
||||
integer :: n, rnum
|
||||
|
||||
s(0) = seed
|
||||
s(1) = 1
|
||||
|
||||
do n = 2, 54
|
||||
s(n) = mod(s(n-2) - s(n-1), modulus)
|
||||
if (s(n) < 0) s(n) = s(n) + modulus
|
||||
end do
|
||||
|
||||
do n = 0, 54
|
||||
r(n) = s(mod(34*(n+1), 55))
|
||||
end do
|
||||
|
||||
do n = 1, 165
|
||||
rnum = subrand()
|
||||
end do
|
||||
|
||||
end subroutine initgen
|
||||
|
||||
integer function subrand()
|
||||
integer, save :: p1 = 0
|
||||
integer, save :: p2 = 31
|
||||
|
||||
r(p1) = mod(r(p1) - r(p2), modulus)
|
||||
if (r(p1) < 0) r(p1) = r(p1) + modulus
|
||||
subrand = r(p1)
|
||||
p1 = mod(p1 + 1, 55)
|
||||
p2 = mod(p2 + 1, 55)
|
||||
|
||||
end function subrand
|
||||
end module subgenerator
|
||||
|
||||
program subgen_test
|
||||
use subgenerator
|
||||
implicit none
|
||||
|
||||
integer :: seed = 292929
|
||||
integer :: i
|
||||
|
||||
call initgen(seed)
|
||||
do i = 1, 10
|
||||
write(*,*) subrand()
|
||||
end do
|
||||
|
||||
end program
|
||||
|
|
@ -1,23 +1,23 @@
|
|||
/*REXX pgm uses a subtractive generator, creates a seq of random numbers*/
|
||||
numeric digits 20; billion = 10**9; s.0 = 292929; s.1 = 1
|
||||
cI = 55; cJ = 24; cP = 34
|
||||
do i=2 to cI-1
|
||||
s.i=mod(s(i-2)-s(i-1),billion)
|
||||
end /*i*/
|
||||
do j=0 to cI-1
|
||||
r.j = s(mod(cP*(j+1),cI))
|
||||
end /*j*/
|
||||
|
||||
m=219; do k=cI to m; x=k//cI
|
||||
r.x = mod(r(mod(k-cI,cI)) - r(mod(k-cJ,cI)),billion)
|
||||
end /*m*/
|
||||
|
||||
t=235; do n=m+1 to t; y=n//cI
|
||||
r.y = mod(r(mod(n-cI,cI)) - r(mod(n-cJ,cI)),billion)
|
||||
say right(r.y,40)
|
||||
end /*n*/
|
||||
/*REXX pgm uses a subtractive generator, creates a sequence of random #s*/
|
||||
numeric digits 20; s.0=292929; s.1=1; billion=10**9
|
||||
cI=55; cJ=24; cP=34; billion=1e9 /*same*/
|
||||
do i=2 to cI-1
|
||||
s.i=mod(s(i-2) - s(i-1), billion)
|
||||
end /*i*/
|
||||
do j=0 to cI-1
|
||||
r.j=s(mod(cP*(j+1), cI))
|
||||
end /*j*/
|
||||
m=219
|
||||
do k=cI to m; x=k//cI
|
||||
r.x=mod(r(mod(k-cI, cI)) - r(mod(k-cJ, cI)), billion)
|
||||
end /*m*/
|
||||
t=235
|
||||
do n=m+1 to t; y=n//cI
|
||||
r.y=mod(r(mod(n-cI, cI)) - r(mod(n-cJ, cI)), billion)
|
||||
say right(r.y, 40)
|
||||
end /*n*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────subroutines─────────────────────────*/
|
||||
r: parse arg _; return r._
|
||||
s: parse arg _; return s._
|
||||
mod: procedure; arg a,b; return ((a // b) + b) // b
|
||||
/*──────────────────────────────────one─liner subroutines───────────────*/
|
||||
mod: procedure; parse arg a,b; return ((a // b) + b) // b
|
||||
r: parse arg _; return r._
|
||||
s: parse arg _; return s._
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue