Linear congruential generator: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
(add Standard ML) |
||
Line 3,295: | Line 3,295: | ||
spn:19> BSD_rand() |
spn:19> BSD_rand() |
||
= 229283573</lang> |
= 229283573</lang> |
||
=={{header|Standard ML}}== |
|||
<lang sml>local |
|||
open Word32 |
|||
in |
|||
fun bsdLcg (seed : int) : int = |
|||
toInt (andb (0w1103515245 * fromInt seed + 0w12345, 0wx7fffffff)) |
|||
fun mscLcg (seed : word) : int * word = |
|||
let |
|||
val state = andb (0w214013 * seed + 0w2531011, 0wx7fffffff) |
|||
in |
|||
(toInt (>> (state, 0w16)), state) |
|||
end |
|||
end</lang> |
|||
;Test code<nowiki>:</nowiki> |
|||
<lang sml>fun test1 rand = |
|||
(print (" " ^ Int.toString rand); rand) |
|||
fun test2 (rand, state) = |
|||
(print (" " ^ Int.toString rand); state) |
|||
fun doTimes (_, 0, state) = () |
|||
| doTimes (f, n, state) = doTimes (f, n - 1, f state) |
|||
val () = print "BSD:\n" |
|||
val () = doTimes (test1 o bsdLcg, 7, 0) |
|||
val () = print "\nMSC:\n" |
|||
val () = doTimes (test2 o mscLcg, 7, 0w0) |
|||
val () = print "\n"</lang> |
|||
{{out}} |
|||
<pre>BSD: |
|||
12345 1406932606 654583775 1449466924 229283573 1109335178 1051550459 |
|||
MSC: |
|||
38 7719 21238 2437 8855 11797 8365</pre> |
|||
=={{header|Stata}}== |
=={{header|Stata}}== |