Interesting randomness

For test results, bug reports, announcements of new builds etc.

Moderators: winston, another_commander, Getafix

Post Reply
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Interesting randomness

Post by Eric Walch »

Last week I noticed that the bad random descriptions in random hits were back. The reason was that I made a fix of doing a random number of string expansions. But because that is very inefficient, I added code that this only happens with Oolite 1.74 and would stop with 1.75. At that time I assumed it would be solved in 1.75. But not. :twisted: I tried saving and restoring seeds, but the problem was on a different level than I searched.

I got new courage and I spend yesterday and today some time to find one of the causes that resets the random seed for descriptions. e.g. the following code resets the seed.

Code: Select all

log(System.systemNameForID(Math.floor(Math.random()*256)) + ": "+expandDescription("%R"))
The "%R" expands always to the same name when first calculating the name for an other system. It becomes only random when using the local system.ID
Strange enough did

Code: Select all

log(System.systemNameForID(Math.floor(Math.random()*256)) + ": "+expandDescription("[nom1]"))
not reset the seed.

Diving more deep into the code, I see that anything with "%" in the description makes that the systemdata for the current system is generated. Normally it is cached, but when first accessing a different system, the cache has to be updated with the local system and here the seeds needs to be reset. (Resetting happens in 'DescriptionForSystem()')

I tested following fix:

Code: Select all

Index: /Users/ericwalch/Oolite/trunk/src/Core/Universe.m
===================================================================
--- /Users/ericwalch/Oolite/trunk/src/Core/Universe.m	(revision 4642)
+++ /Users/ericwalch/Oolite/trunk/src/Core/Universe.m	(working copy)
@@ -5867,6 +5867,7 @@
 	sCachedSystemData = nil;
 	cachedSeed = s_seed;
 	
+	RNG_Seed saved_seed = currentRandomSeed();
 	NSMutableDictionary* systemdata = [[NSMutableDictionary alloc] init];
 	
 	OOGovernmentID government = (s_seed.c / 8) & 7;
@@ -5917,7 +5918,7 @@
 	{
 		[systemdata setObject:DescriptionForSystem(s_seed,[systemdata oo_stringForKey:KEY_NAME]) forKey:KEY_DESCRIPTION];
 	}
-	
+	if (useCache) setRandomSeed(saved_seed);
 	sCachedSystemData = [systemdata copy];
 	[systemdata release];
Two lines are added to the generating of system data. First store the old seed and at the and set it back when in mode were the cache is used. On the first system setup "useCache" is false, so no seed is restored and there is no risk of disturbing the old process of setting up a system. And after that the setup process never accesses an other system, so all info comes from the cache and my added code is never reached. Only when accessing system data from another system (By script or on the F7 page), There is need for building a new cache. But than the current system is already fully set up and this has no effect.

I try it a few days on my system and than do a commit. Unless anybody now already sees were this code could lead to a bug.
At a first test I see no wrong planet positions or different colors for planets.
Post Reply