Skip to content

Instantly share code, notes, and snippets.

@joonjoonjoon
Created May 28, 2014 16:17
Show Gist options
  • Select an option

  • Save joonjoonjoon/13872ab7777a553a3fd6 to your computer and use it in GitHub Desktop.

Select an option

Save joonjoonjoon/13872ab7777a553a3fd6 to your computer and use it in GitHub Desktop.
workaround for FlxRandom
private static var seed:Int;
private static var increment:Int;
public static function GetNextSeededRandom():Float
{
increment++;
FlxRandom.globalSeed = seed;
var result:Float = 0;
for (i in 0...increment)
{
result = FlxRandom.float();
}
return result;
}
public static function SetSeed(newSeed:Int):Void
{
increment = 0;
seed = newSeed;
}
@JoeCreates
Copy link

Hey @joonjoonjoon. I'm not sure this is necessary, and it could certainly get very inefficient and slow when your increment gets big. I'm wondering if there might be an easier way . . . Have you considered something like this?

private var mySeed:Int;

override public function update():Void {
    FlxRandom.currentSeed = mySeed;

    // --- Use much FlxRandom ---

    mySeed = FlxRandom.currentSeed;
}

Or if you also need the global seed (as in, the currentSeed that applies globally) to persist:

private var mySeed:Int;

override public function update():Void {
    var globalSeed:Int = FlxRandom.currentSeed; // Store current seed
    FlxRandom.currentSeed = mySeed; // Set current seed for this specific replay

    // --- Use much FlxRandom ---

    mySeed = FlxRandom.currentSeed; // Update current seed for this replay
    FlxRandom.currentSeed = globalSeed; // Resume using global seed
}

You could move this behavior into a separate RandomSeed class or something:

private var mySeed:RandomSeed;

override public function update():Void {
    mySeed.load();

    // --- Use much FlxRandom ---

    mySeed.unload();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment