Atomic, Peristent and Pseudo Random Number Generator in GameSparks (II)

One interesting thing about pseudo random numbers is, given a seed, the sequence of random numbers is predictable, so you can know what are the next numbers in the sequence.

I completely forgot to add the posibility of peeking the next numbers in my "Atomic, Peristent and Pseudo Random Number Generator". I've modified the event, adding one parameter "numPeeks".
Now in the scriptData you have a key "numPeeks" with the numPeeks returned (is limited to 10 for security reasons) and one key per num peeked: "peek1", "peek2", and so on.

So here it is:

//Based on:

/* We pass an object because we want the changes on the seed to be an output parameter
   That way we can store the seed in a runtime collection and use it again when needed, 
   keeping the sequence of random numbers from one event execution to another event execution.*/
var PseudoRandomSeed = function(obj) {
    return function() { //if peek true we will only look the value, but  seed won't be modified
        var newSeed = Math.sin(obj.seed) * 10000;
        var result = newSeed - Math.floor(newSeed);
        obj.seed = newSeed;
        return result;

var sessionId   = Spark.getData().sessionId;
var numPeeks    = Spark.getData().numPeeks;

//Mutex: We don't want other user to get a random while this is running
Spark.lockKey("AtomicRandom", 100);

var seedFromDB = GetMatchSeed(sessionId);
var lastSeed = undefined;
//If there is no seed get the session ID as the prime seed
if (seedFromDB == null || seedFromDB == undefined)
    lastSeed = parseInt(sessionId, 16); //I'm supossing the sessionID is an hexadecimal number
else //Otherwise, we used the stored seed
    lastSeed = seedFromDB;
var myObj = {
    seed: lastSeed
var PseudoRandom = PseudoRandomSeed(myObj);

//Generate randomNumber and store it in the scriptData
var randomNumber        = PseudoRandom();
var setMatchSeedSuccess = SetMatchSeed(sessionId, myObj.seed);

Spark.setScriptData("randomNumber", randomNumber);

//Generate peeks for the next randoms
if (numPeeks != null && numPeeks != undefined)
    if (numPeeks > 10) //security check: Max peeks allowed is 10
        numPeeks = 10;
    Spark.setScriptData("numPeeks", numPeeks);
    for (i = 0; i < numPeeks; ++i)
        Spark.setScriptData("peek" + i , PseudoRandom());



Entradas populares