save random number sequence

i couldnt find a good way to express the subject in the title so, if what you are about to read is not what you thought you would from the title, sorry!!!

read about the random number stuff, algorithms etc,

what i didnt find anywhere is how to save the place the sequence has reached so far, so the code can continue from then on

the game asks the RandomNumberGenerator for a number
the first generated number is returned
for the second request the RNG returns the second number the specific algorithm generates
and so on

after the game stops, i want it for the RNG to be able to know how many numbers it have generated so far, so as to continue the process of the “generation” from that place of the sequence

the stupid way is to save the number of numbers generated, and during the game initiallization ask for the RNG to create (and dumb) as many numbers as have been created…

do i make sense?

thank you

(my guess is that there are algorithms that allow you to pick the place of sequence you want it to start generating from and not from place 1 all the time… but i cant seem to find any, if only i were smarter i would create my own…)

Take the java.util.Random source code, add a getter, rename the class, Bob’s your uncle. Assuming you don’t mind the licensing issues this throws up.

Generators each have a state. You simply need to save and restore this state information to continue the sequence.

Check out this thread:
http://www.java-gaming.org/index.php/topic,18426.0.html

O.K. I’ll bite: Why do you want to do this?

You could use pseudorandom;


       long randomSeed=0;// or whatever seed you like

	public float rnd()
	{
		int bits=16;

		// generate random number between 0-65535
		randomSeed = (randomSeed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
		float r=(float)(randomSeed>>>(48-bits));

		// return 0-0.99f
		return (r/65536);
	}

This code will always generate the same sequence of ‘random’ numbers from a given seed. With this you can go back to any previous states by replacing the current seed with one saved from an earlier state.

[quote]Generators each have a state. You simply need to save and restore this state information to continue the sequence.
[/quote]
dont really get it, the only info tha a generator holds is the seed
(of course i am wrong, but thats what i have managed to grasp so far)

[quote] Check out this thread:
http://www.java-gaming.org/index.php/topic,18426.0.html
[/quote]
thanks for the link (didnt really get much, but thanks anyway)

[quote]O.K. I’ll bite: Why do you want to do this?
[/quote]
in my (miserable) attempt to creating a Dungeon Master assistant software
i have noticed that people use their laptops and notebooks and netbooks and iPhones and…
and ofcourse they are really spoiling the fun but since they do it, why shouldnt i write a software?

the main idea is this: all (pseydo) random number generator algorithms try to fulfill some statistical criteria that apply to (trully) random numbers. but, as the Law of Large Numbers dictates, the sample (the random numbers used in the game) must be larger than countable in order for the hypothesis to be true (randomness). since the game of DnD uses a lot of dice , i believe that it can improve on the illusion of random numbers , in comparison to just changing the seed and using the same math from the beginning. for example having a day of luck and another of bad luck instead of having an averge day everytime

[quote]This code will always generate the same sequence of ‘random’ numbers from a given seed. With this you can go back to any previous states by replacing the current seed with one saved from an earlier state.
[/quote]
if only i were smarter i would be able to trully understand this piece of code
it is what i am looking for, i think
and 65.000 are more than enough random numbers

thank you both

For D&D you really don’t need anything except pseudo randomness. Just initialize your seed every time you start up with the current time in milliseconds or nanoseconds. This more than suffices to keep every session different and seemingly balanced in terms of range.

The state of a random number generator is the seed. A generator typically doesn’t store anything else.

Lol! Me too! Don’t worry about the maths, just think of it as a ‘black box’ that given a start number will always give you the same sequence of random numbers.
NB the code I posted returns a float between 0 and 0.99 (like Math.random()) not as you suggested 0 and 65…

You might find this simpler:

static private int randomSeed = (int)System.currentTimeMillis();

/**
 * Returns a random number between 0 (inclusive) and the specified value (inclusive).
 * @param range Must be >= 0.
 */
static public final int random (int range) {
	int seed = randomSeed * 1103515245 + 12345;
	randomSeed = seed;
	return ((seed >>> 15) * (range + 1)) >>> 17;
}

Props go to Riven for this code! The value of randomSeed determines the next random number. So at any time you can store the value of randomSeed and continue or replay the random numbers generated.

I have a feeling, as Demonpants mentioned, that you don’t actually have a need to store the seed. Just initialize with System.currentTimeMillis() and you’ll get random numbers every time.

For similar code to generate other types (eg floats):
http://www.java-gaming.org/index.php/topic,21135.0.html

thank you for the code (and pass my thanks to Riven)

being able to continue the sequence of random numbers (even if its not needed for DnD) strikes me as beautiful, so i want to do it.
thats why i tried, i couldnt, i asked and i learned

(maybe i or somenone else will find a good reason of using it apart from DnD)

Simon’s code is essentially exactly the same as Riven’s code…

There was a thread somewhere with a bunch of these posted. I put one up too. It’s all the same idea, though. Basically you put this hard to understand bit shifting into your random function and then never look at it again. It works fine.

The shifts make a fixed point multiply (they add up to 32). It’s broken in two parts to allow space for the result of the multiply. The plus one is to deal with truncation.