tag:blogger.com,1999:blog-7732292498484945518.post1872325912343702993..comments2019-01-10T19:18:38.377-05:00Comments on The Classics in the Modern Idiom: The Troll Room - Randomness in the Z-machineMike Gregerhttp://www.blogger.com/profile/06764871992548118867noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-7732292498484945518.post-54708664759580954952009-09-18T21:16:29.235-04:002009-09-18T21:16:29.235-04:00I felt adding a check for unseeded state and seedi...I felt adding a check for unseeded state and seeding with a constant doesn't really add any value. I could require a seed in the constructor but I'm not that concerned about it really. It gets seeded at machine startup and the code that does so is very simple.Mike Gregerhttps://www.blogger.com/profile/06764871992548118867noreply@blogger.comtag:blogger.com,1999:blog-7732292498484945518.post-29260379386022327012009-09-18T18:20:57.326-04:002009-09-18T18:20:57.326-04:00If you're concerned about forgetting to seed t...If you're concerned about forgetting to seed the generator, why not throw an exception? For that matter, why allow the generator to be constructed in an unseeded state? Couldn't you require a seed in the constructor?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7732292498484945518.post-51532406941213196582009-09-18T16:00:41.508-04:002009-09-18T16:00:41.508-04:00Speaking of the "classics", here is an i...Speaking of the "classics", here is an implementation using that exact same very poor algorithm that many systems' ANSI C rand() function use:<br /><br /> class AnsiBrokenRandomNumberGenerator : RandomNumberGenerator <br /> {<br /> private int randx;<br /><br /> protected override short GetNext()<br /> {<br /> return unchecked((short)(((randx = randx * 1103515245 + 12345) >> 16) & 0x7FFF));<br /> }<br /><br /> protected override void SeedGenerator()<br /> {<br /> randx = this.Seed;<br /> }<br /> }<br /><br />Provided in case you want to make 'Balances' unwinnable.Random832noreply@blogger.comtag:blogger.com,1999:blog-7732292498484945518.post-19414174465117670732009-09-18T13:09:47.723-04:002009-09-18T13:09:47.723-04:00Thanks Eric.
It's interesting that most publi...Thanks Eric.<br /><br />It's interesting that most published algorithms of this kind seem to be written by people allergic to giving variables intelligble names.<br /><br />In my implementation of the MT I tried to find decent names from the documentation or code comments. I fixed most of them except for a couple, e.g. kk and mag01. I suspect mag stands for magic. :)Mike Gregerhttps://www.blogger.com/profile/06764871992548118867noreply@blogger.comtag:blogger.com,1999:blog-7732292498484945518.post-19969349158360100632009-09-18T12:44:49.200-04:002009-09-18T12:44:49.200-04:00The Random algorithm in the .NET Framework is base...The Random algorithm in the .NET Framework is based on the "ran3" algorithm here: http://www.fizyka.umk.pl/nrbook/c7-1.pdf, which is in turn based on the algorithm from Knuth's "Seminumerical Algorithms".Eric Lipperthttp://blogs.msdn.com/ericlippertnoreply@blogger.comtag:blogger.com,1999:blog-7732292498484945518.post-72999992085683434652009-09-17T22:40:58.145-04:002009-09-17T22:40:58.145-04:00You could definitely use the Random class in the d...You could definitely use the Random class in the derived implementation. The abstract base is used to enforce the z-machine specific behavior.<br /><br />One reason I used the MT is because it is generally considered a high quality pseudo-random generator. A remark in the specification is what first got me started thinking about it: "It is dangerous to rely on the ANSI C random number routines, as some implementations of these are very poor. This has made some games (in particular, 'Balances') unwinnable on some Unix ports of Zip."<br /><br />I don't know how good the algorithm used by Random is (I couldn't find any detailed information on it), but I've done some simple testing using dice simulations with Random and the results aren't fantastic.Mike Gregerhttps://www.blogger.com/profile/06764871992548118867noreply@blogger.comtag:blogger.com,1999:blog-7732292498484945518.post-36430839551530720132009-09-17T20:30:50.287-04:002009-09-17T20:30:50.287-04:00Why didn't you use the Random class?Why didn't you use the Random class?configuratorhttp://blog.tapuchim.comnoreply@blogger.com