diff options
author | rubidium <rubidium@openttd.org> | 2009-02-25 01:21:50 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2009-02-25 01:21:50 +0000 |
commit | 1ad50ce4e6ed74c8eeb89a1f2780e6ed58a72b2b (patch) | |
tree | 0955433146cc59335ddaac1d6e08eb177a9ead61 /src/3rdparty/squirrel/samples/generators.nut | |
parent | 66a8c0a1325e1f8717150dc6bbe1bcb4fb458bef (diff) | |
download | openttd-1ad50ce4e6ed74c8eeb89a1f2780e6ed58a72b2b.tar.xz |
(svn r15578) -Change: unexternalise squirrel.
Diffstat (limited to 'src/3rdparty/squirrel/samples/generators.nut')
-rw-r--r-- | src/3rdparty/squirrel/samples/generators.nut | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/3rdparty/squirrel/samples/generators.nut b/src/3rdparty/squirrel/samples/generators.nut new file mode 100644 index 000000000..9853e0964 --- /dev/null +++ b/src/3rdparty/squirrel/samples/generators.nut @@ -0,0 +1,42 @@ +/* +*Random number function from The Great Computer Language shootout +*converted to a generator func +*/ + +function gen_random(max) { + local last=42 + local IM = 139968; + local IA = 3877; + local IC = 29573; + for(;;){ //loops forever + yield (max * (last = (last * IA + IC) % IM) / IM); + } +} + +local randtor=gen_random(100); + +print("RAND NUMBERS \n") + +for(local i=0;i<10;i+=1) + print(">"+resume randtor+"\n"); + +print("FIBONACCI \n") +function fiboz(n) +{ + local prev=0; + local curr=1; + yield 1; + + for(local i=0;i<n-1;i+=1) + { + local res=prev+curr; + prev=curr; + yield curr=res; + } + return prev+curr; +} + +foreach(val in fiboz(10)) +{ + ::print(">"+val+"\n"); +} |