Linear congruential generator: Difference between revisions

Added Easylang
(add RPL)
(Added Easylang)
 
(3 intermediate revisions by 3 users not shown)
Line 1,162:
35439
</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
func mul32 a b .
# to avoid overflow with 53bit integer precision with double
ah = a div 0x10000
al = a mod 0x10000
bh = b div 0x10000
bl = b mod 0x10000
return al * bl + al * bh * 0x10000 + bl * ah * 0x10000
.
global state_bsd state_ms .
func rand_bsd .
state_bsd = (mul32 1103515245 state_bsd + 12345) mod 0x80000000
return state_bsd
.
func rand_ms .
state_ms = (214013 * state_ms + 2531011) mod 0x80000000
return state_ms div 0x10000
.
for i = 1 to 5
print rand_bsd
.
print ""
for i = 1 to 5
print rand_ms
.
</syntaxhighlight>
 
{{out}}
<pre>
12345
1406932606
654583775
1449466924
229283573
 
38
7719
21238
2437
8855
</pre>
 
=={{header|EDSAC order code}}==
The first version of this solution had trouble with the "sandwich digit". As pointed out by Wilkes, Wheeler & Gill (1951 edition, page 26), a 35-bit constant cannot be loaded via pseudo-orders if the middle bit (sandwich digit) is 1. One workaround, adopted in the EDSAC solution to the Babbage Problem, is to use the negative of the constant instead. The alternative, which WWG evidently preferred and which is used in the LCG solution posted here, is to load 35-bit constants via the library subroutine R9.
Line 1,727 ⟶ 1,770:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Linear_congruential_generator}}
 
'''Solution'''
 
'''Definitions'''
 
[[File:Fōrmulæ - Linear congruential generator 01.png]]
 
[[File:Fōrmulæ - Linear congruential generator 02.png]]
 
'''Test case'''
 
[[File:Fōrmulæ - Linear congruential generator 03.png]]
 
[[File:Fōrmulæ - Linear congruential generator 04.png]]
 
=={{header|Go}}==
Line 1,894 ⟶ 1,951:
 
=={{header|jq}}==
The Go implementation of jq (gojq) supports unlimited-precision integer arithmetic and therefore linear congruential generators (LCGs) can be trivially written for gojq.
Currently, jq arithmetic is based on IEEE 754 64-bit numbers. As a result, it is trivial to implement the Microsoft linear congruential generator (LCG), but the BSD generator requires some kind of "big integer" support. In this section, therefore, we first present functions to support the Microsoft LCG, and then present functions to support the LCG on the assumption that a suitable jq "BigInt" library is available.
 
The C implementation of jq, however, currently uses IEEE 754 64-bit numbers for arithmetic, so a BSD generator for the C implementation of jq would require some kind of "big integer" support.
 
In this entry, therefore, we first present functions for the Microsoft LCG that can be used with jq or gojq, and then present functions to support the BSD generator on the assumption that a suitable "BigInt" library is available.
====Microsoft LCG====
<syntaxhighlight lang="jq"># 15-bit integers generated using the same formula as rand()
Line 3,865 ⟶ 3,926:
{{libheader|Wren-fmt}}
Some of the intermediate calculations here require integers >= 2^53 so we need to use BigInt.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
import "./fmt" for Fmt
 
// basic linear congruential generator
1,969

edits