Linear congruential generator: Difference between revisions

m
→‎{{header|Phix}}: bigatom -> mpfr
m (→‎{{header|REXX}}: added/changed whitespace and comments.)
m (→‎{{header|Phix}}: bigatom -> mpfr)
Line 2,101:
 
=={{header|Phix}}==
{{libheader|bigatommpfr}}
As per the comments, I had to resort to bigatomgmp to get BSDrnd() to work on 32-bit.
<lang Phix>atom seed
 
include builtins/bigatommpfr.e
bigatom ba_seed
 
function BSDrnd()
-- oh dear, native only works on 64-bit,
ba_seed = ba_remainder(ba_add(ba_multiply(1103515245,ba_seed),12345), #8000_0000)
--function BSDrnd() -- (only works on 64-bit, as per ERRE and UCBLogo above on 32-bit)...
return ba_seed
-- 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 ba_seedseed
end function
 
--function BSDrnd() -- (only works on 64-bit, as per ERRE and UCBLogo above on 32-bit)
-- seed = remainder(1103515245 * seed + 12345, #8000_0000)
-- return seed
--end function
 
function MSrnd()
seed = and_bits(seed*214013+2531011,#7FFFFFFF)
return floor(seed/power(2,16))
end function
 
--seed = 0
ba_seed = ba_new(0)
?"BSDrnd"
--for i=1 to 10 do ?printf(1,"%d\n",BSDrnd()) end for -- (64-bit only)
for i=1 to 10 do puts(1,ba_sprintf("%B\n",BSDrnd())) end for
seed = 0
?"MSrnd"
for i=1 to 10 do ?printf(1,"%d\n",MSrnd()) end for</lang>
{{out}}
<pre>
7,794

edits