[Kde-games-devel] Reproducing random numbers

Kevin Krammer kevin.krammer at gmx.at
Mon Apr 13 10:50:18 CEST 2009


On Monday 13 April 2009, Ian Wadham wrote:
> While working on the record/replay feature for KGoldrunner I have
> struck a couple of problems with random numbers.  I would
> appreciate any help or reassurance you can offer re the solutions
> I have adopted.
>
> Enemies in KGoldrunner depend on random numbers to decide
> where to reappear after being killed and when to pick up or drop
> gold.  Up till now the game has been using C library rand() (man
> 3 rand) with a seed based on the time() function (man 2 time), so
> that the levels play differently from one time to another.
>
> That's all very well, but how do you get a replayed level to run exactly
> the same as when it was recorded?  Random number generators
> appear to have no way to "read out" a seed.  The solution adopted
> was to generate a random integer when "live" play of a level is about to
> start, then re-inject that number as the seed and also save it in the
> "recording".  When playing back the recording, read in and use the
> same seed.  This seems a bit dodgy, but it works.

I think it is still based on too many assumptions for a reliable replay 
feature.
While it is true that pseudo random generators such as rand() will generate 
the same sequence when being seeded with the same value, there is AFAIK no 
gurantee that the algorithm stays the same during system upgrades, or 
generates the same sequence on a different device or platform (assuming 
recordings can be copied).

In case you are using the same random value to control more than one aspect, 
e.g. to determine the direction of more than one enemy, you might want to 
record the values as you retrieve them.

If one random value is only used for one decision, you could think about 
recording the decision instead.

Cheers,
Kevin

-- 
Kevin Krammer, KDE developer, xdg-utils developer
KDE user support, developer mentoring
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://mail.kde.org/pipermail/kde-games-devel/attachments/20090413/03c30322/attachment.sig 


More information about the kde-games-devel mailing list