<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wiki.spheredev.org/index.php?action=history&amp;feed=atom&amp;title=API%3ARNG%3A%3Astate</id>
		<title>API:RNG::state - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.spheredev.org/index.php?action=history&amp;feed=atom&amp;title=API%3ARNG%3A%3Astate"/>
		<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=API:RNG::state&amp;action=history"/>
		<updated>2026-06-03T10:53:55Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.29.0</generator>

	<entry>
		<id>http://wiki.spheredev.org/index.php?title=API:RNG::state&amp;diff=10124&amp;oldid=prev</id>
		<title>Bruce Pascoe: Created page with &quot;{{DISPLAYTITLE:RNG::state}}  '''&lt;tt&gt;RNG::state&lt;/tt&gt;''' is an accessor property which allows you to get or set the current state of the random number generator instance.  === U...&quot;</title>
		<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=API:RNG::state&amp;diff=10124&amp;oldid=prev"/>
				<updated>2017-08-09T15:31:42Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;{{DISPLAYTITLE:RNG::state}}  &amp;#039;&amp;#039;&amp;#039;&amp;lt;tt&amp;gt;RNG::state&amp;lt;/tt&amp;gt;&amp;#039;&amp;#039;&amp;#039; is an accessor property which allows you to get or set the current state of the random number generator instance.  === U...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{DISPLAYTITLE:RNG::state}}&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;tt&amp;gt;RNG::state&amp;lt;/tt&amp;gt;''' is an accessor property which allows you to get or set the current state of the random number generator instance.&lt;br /&gt;
&lt;br /&gt;
=== Usage ===&lt;br /&gt;
&lt;br /&gt;
 ''current_state'' = ''rng_object''.'''state''';&lt;br /&gt;
 ''rng_object''.'''state''' = ''new_state'';&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== API Information ==&lt;br /&gt;
&lt;br /&gt;
=== Description ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;'''RNG::state'''&amp;lt;/tt&amp;gt; is a 32-byte hexadecimal string that encodes the exact state of the &amp;lt;tt&amp;gt;RNG&amp;lt;/tt&amp;gt; instance.  If you save the value of this property to a file and load it in a later session, you can resume a random number sequence from where you left off.  This is distinct from seeding (see &amp;lt;tt&amp;gt;[[API:RNG::fromSeed|RNG.fromSeed()]]&amp;lt;/tt&amp;gt;), which starts a sequence from the beginning.&lt;br /&gt;
&lt;br /&gt;
One use of this is to combat &amp;quot;save scumming&amp;quot;, where a player saves a game before a randomly-determined event and reloads until they get a result they like (in Pokémon, for instance).  If you save the state of the random number generator along with the other game data, the RNG becomes much harder to exploit since the same sequence of numbers will be generated again after the reload.  This is also why Sphere allows you to construct multiple &amp;lt;tt&amp;gt;RNG&amp;lt;/tt&amp;gt; instances: by having separate instances for different events and persisting their states across saves, you can combat RNG manipulation.&lt;br /&gt;
&lt;br /&gt;
=== Examples ===&lt;br /&gt;
&lt;br /&gt;
==== Saving ====&lt;br /&gt;
&lt;br /&gt;
 let save = {&lt;br /&gt;
     name:     heroGuy.name,&lt;br /&gt;
     level:    heroGuy.level,&lt;br /&gt;
     rngState: myRNG.state,&lt;br /&gt;
     // etc...&lt;br /&gt;
 };&lt;br /&gt;
 let saveData = JSON.stringify(save);  // JSON encode&lt;br /&gt;
 FS.writeFile('~/saveGame.json', saveData);&lt;br /&gt;
&lt;br /&gt;
==== Loading ====&lt;br /&gt;
&lt;br /&gt;
 let saveData = FS.readFile('~/saveGame.json');&lt;br /&gt;
 let save = JSON.parse(saveData);  // JSON decode&lt;br /&gt;
 heroGuy.name = save.name;&lt;br /&gt;
 heroGuy.level = save.level;&lt;br /&gt;
 myRNG = RNG.fromState(save.state);  // or: myRNG.state = save.state;&lt;br /&gt;
 // etc...&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
* [[API:RNG.fromSeed|RNG.fromSeed()]]&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Sphere 2 API]]&lt;/div&gt;</summary>
		<author><name>Bruce Pascoe</name></author>	</entry>

	</feed>