{"id":443,"date":"2013-09-29T19:05:40","date_gmt":"2013-09-30T01:05:40","guid":{"rendered":"http:\/\/rrbits.com\/epb\/?p=443"},"modified":"2013-09-29T19:09:06","modified_gmt":"2013-09-30T01:09:06","slug":"repeatable-pseudo-random-number-generator","status":"publish","type":"post","link":"https:\/\/rrbits.com\/epb\/2013\/09\/29\/repeatable-pseudo-random-number-generator\/","title":{"rendered":"Repeatable Pseudo Random Number Generator (PHP)"},"content":{"rendered":"<p>I wrote a simple class to get repeatable random numbers out of an input seed.\u00a0 I&#8217;ll post it here in case someone else would find such a thing useful.\u00a0 It is based on SHA1 rounds. After a number of any kind is fetched, another round of SHA1 is applied.<\/p>\n<p>Download it here: <a href=\"\/downloads\/rprng-r2.zip\">RepeatablePseudoRNG<\/a><\/p>\n<p>The class has 3 different methods for retrieving random numbers.<\/p>\n<p><!--more--><\/p>\n<h3>Methods:<\/h3>\n<p><strong>Constructor<\/strong>(<em>seed [null]<\/em>)<\/p>\n<p>Provide your initial seed when you instantiate a new object.<\/p>\n<p><strong>fetch_int<\/strong>(<em>min<\/em>, <em>max<\/em>)<\/p>\n<p>Fetch an integer between min and max. This is inclusive and the max range is limited to 31 bits on most systems.<\/p>\n<p><strong>fetch_float<\/strong>(<em>min<\/em>,<em>max<\/em>)<\/p>\n<p>Fetch a floating point number between min and max. Like, fetch_int, this is inclusive.<\/p>\n<p><strong>rand<\/strong>()<\/p>\n<p>Fetch a floating point number between o and 1.\u00a0 Also inclusive, though getting exactly 0 or 1 should be rare.<\/p>\n<p><strong>perform_rounds<\/strong>(<em>roundcount<\/em>)<\/p>\n<p>Update the seed a specific number of times, without actually generating any numbers.<\/p>\n<h3>Usage example:<\/h3>\n<pre>require 'rprng.php';\r\n$rng = new RepeatablePseudoRNG('Totori');\r\n\/\/Generate some random numbers.\r\nfor($i = 0; $i &lt; 10; $i++)\r\n{\r\n   echo $rng-&gt;fetch_int(1,10),\"\\n\";\r\n}<\/pre>\n<p>The above would output a list similar to the following each time it is run:<\/p>\n<pre>2\r\n8\r\n3\r\n5\r\n2\r\n6\r\n9\r\n2\r\n5\r\n1<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I wrote a simple class to get repeatable random numbers out of an input seed.\u00a0 I&#8217;ll post it here in case someone else would find such a thing useful.\u00a0 It is based on SHA1 rounds. After a number of any kind is fetched, another round of SHA1 is applied. Download it here: RepeatablePseudoRNG The class&#8230;  <a class=\"excerpt-read-more\" href=\"https:\/\/rrbits.com\/epb\/2013\/09\/29\/repeatable-pseudo-random-number-generator\/\" title=\"ReadRepeatable Pseudo Random Number Generator (PHP)\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[4249],"class_list":["post-443","post","type-post","status-publish","format-standard","hentry","category-php","tag-rprng"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/rrbits.com\/epb\/wp-json\/wp\/v2\/posts\/443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rrbits.com\/epb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rrbits.com\/epb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rrbits.com\/epb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rrbits.com\/epb\/wp-json\/wp\/v2\/comments?post=443"}],"version-history":[{"count":3,"href":"https:\/\/rrbits.com\/epb\/wp-json\/wp\/v2\/posts\/443\/revisions"}],"predecessor-version":[{"id":448,"href":"https:\/\/rrbits.com\/epb\/wp-json\/wp\/v2\/posts\/443\/revisions\/448"}],"wp:attachment":[{"href":"https:\/\/rrbits.com\/epb\/wp-json\/wp\/v2\/media?parent=443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rrbits.com\/epb\/wp-json\/wp\/v2\/categories?post=443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rrbits.com\/epb\/wp-json\/wp\/v2\/tags?post=443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}