from https://rosettacode.org/wiki/Subtractive_generator#M2000_Interpreter
Class Subtractive_generator {
private:
long mod10=10^9, si=0, sj=0
dim state(55) as long
module subrand_seed (p1 as long) {
long i, j=21, p2=1, d
.state(0)=p1 mod .mod10
for i=1 to 54
if j>=55 then j-=55
.state(j)=p2
p2=p1-p2
if p2<0 then p2+=.mod10
p1=.state(j)
j+=21
next
.si<=0
.sj<=24
}
public:
property counter {
value
}=55&
module advance (n) {
for i=1 to n:d=.subrand():next
}
function subrand {
long x
if .si=.sj then .subrand_seed 0
.si--:if .si<0 then .si<=54
.sj--:if .sj<0 then .sj<=54
x=.state(.si)-.state(.sj)
if x<0 then x+=.mod10
.state(.si)=x
=x
.[counter]++
}
class:
module Subtractive_generator {
.subrand_seed
}
}
sg=Subtractive_generator(292929&)
sg.advance 220-55
sg1=sg ' copy of sg
for i=1 to 10
print str$(sg.counter, "\r\[000\] = ")+sg.subrand()
next
print "== copy ==" ' print same numbers as before
for i=1 to 10
print str$(sg1.counter, "\r\[000\] = ")+sg1.subrand()
next
r[220] = 467478574 r[221] = 512932792 r[222] = 539453717 r[223] = 20349702 r[224] = 615542081 r[225] = 378707948 r[226] = 933204586 r[227] = 824858649 r[228] = 506003769 r[229] = 380969305 == copy == r[220] = 467478574 r[221] = 512932792 r[222] = 539453717 r[223] = 20349702 r[224] = 615542081 r[225] = 378707948 r[226] = 933204586 r[227] = 824858649 r[228] = 506003769 r[229] = 380969305
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου
You can feel free to write any suggestion, or idea on the subject.