RosettaCodeData/Task/Linear-congruential-generator/Phix/linear-congruential-generator.phix
2019-09-12 10:33:56 -07:00

32 lines
914 B
Text

atom seed
include builtins/mpfr.e
function BSDrnd()
-- oh dear, native only works on 64-bit,
-- as per ERRE and UCBLogo above on 32-bit...
-- seed = remainder(1103515245 * seed + 12345, #8000_0000)
-- so, resort to gmp, with the added twist than both
-- 1103515245 and #8000_0000 are greater than 1GB and
-- therefore a smidge too big & need some extra help...
mpz z = mpz_init(seed),
h8 = mpz_init("2147483648") -- (ie #8000_0000)
mpz_mul_si(z,z,5)
mpz_mul_si(z,z,1103515245/5) -- (do in two <1GB factors)
mpz_add_si(z,z,12345)
mpz_fdiv_r(z,z,h8)
seed = mpz_get_atom(z)
return seed
end function
function MSrnd()
seed = and_bits(seed*214013+2531011,#7FFFFFFF)
return floor(seed/power(2,16))
end function
seed = 0
?"BSDrnd"
for i=1 to 10 do printf(1,"%d\n",BSDrnd()) end for
seed = 0
?"MSrnd"
for i=1 to 10 do printf(1,"%d\n",MSrnd()) end for