Art: Neu-Implementierung
Task: http://rosettacode.org/wiki/Linear_congruential_generator
Beteiligte: James1337
Skript:
Spoiler anzeigen
_BSD_init()
ConsoleWrite("BSD (seed = 0):" & @CRLF)
For $i = 1 To 10
ConsoleWrite(_BSD_rand() & @CRLF)
Next
ConsoleWrite(@CRLF & "Microsoft (seed = 0):" & @CRLF)
_Microsoft_init()
For $i = 1 To 10
ConsoleWrite(_Microsoft_rand() & @CRLF)
Next
Func _BSD_init($seed=0)
LCG("r", $seed)
LCG("a", 1103515245)
LCG("c", 12345)
LCG("m", 2^31)
EndFunc
Func _BSD_rand()
Return LCG()
EndFunc
Func _Microsoft_init($seed=0)
LCG("r", $seed)
LCG("a", 214013)
LCG("c", 2531011)
LCG("m", 2^31)
EndFunc
Func _Microsoft_rand()
Return Int(LCG() / 2^16)
EndFunc
Func LCG($0="", $1=0)
Local Static $r = 0, $a = 0, $c = 0, $m = 0
Switch $0
Case "r"
$r = $1
Case "a"
$a = $1
Case "c"
$c = $1
Case "m"
$m = $1
EndSwitch
If ($0 <> "") Then Return 0
$r = Mod($a * $r + $c, $m)
Return $r
EndFunc ; LCG() by James1337
Ausgabe:
Spoiler anzeigen
Problem(e): _BSD_rand() liefert falsche Werte, wurde aber meines Wissens nach richtig implementiert.