Essays/Linear Congruential Generator

From J Wiki
Jump to navigation Jump to search

The linear congruential pseudo-random number generator is traditionally the RNG implemented by APL. Its use is now deprecated because of its short period length (at most _1+2^31).

qrl  =: 16807

tick =: [ <.@%~ (* 3 : 'qrl=:(<:2^31)|(7^5)*qrl')@]
roll =: (<:2^31)&tick"0

bigdeal=: 4 : 0
 t=. 0 $ v=. y $~ <.1.11*x
 while. x > #t do. t=. ~. roll v end.
 x {. t
)

rix  =: i.@[ ([ ,. [ + roll@:-~) ]
deal1=: [ {. <@~."1@|.@rix C. i.@-@]
deal =: deal1 ` bigdeal @. (< 0.01&*)

For example:

   roll 2 4$1e6
131537 755605 458650 532767
218959  47044 678864 679296

   17 deal 17
1 9 7 2 12 11 5 3 8 4 6 15 16 14 0 13 10

   0 ": qrl
1131570933



See also



Contributed by Roger Hui.