[quake3-commits] r1561 - in trunk/code: client qcommon renderer server

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Sun May 31 15:48:28 EDT 2009


Author: thilo
Date: 2009-05-31 15:48:28 -0400 (Sun, 31 May 2009)
New Revision: 1561

Modified:
   trunk/code/client/cl_main.c
   trunk/code/qcommon/common.c
   trunk/code/renderer/tr_noise.c
   trunk/code/server/sv_client.c
   trunk/code/server/sv_init.c
Log:
- Introduce seeding of the random number generator at startup
- Replaced all engine-side occurances of rand() with random()


Modified: trunk/code/client/cl_main.c
===================================================================
--- trunk/code/client/cl_main.c	2009-05-29 03:14:49 UTC (rev 1560)
+++ trunk/code/client/cl_main.c	2009-05-31 19:48:28 UTC (rev 1561)
@@ -1306,13 +1306,9 @@
 		BigShort( cls.updateServer.port ) );
 	
 	info[0] = 0;
-  // NOTE TTimo xoring against Com_Milliseconds, otherwise we may not have a true randomization
-  // only srand I could catch before here is tr_noise.c l:26 srand(1001)
-  // https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=382
-  // NOTE: the Com_Milliseconds xoring only affects the lower 16-bit word,
-  //   but I decided it was enough randomization
-	Com_sprintf( cls.updateChallenge, sizeof( cls.updateChallenge ), "%i", ((rand() << 16) ^ rand()) ^ Com_Milliseconds());
 
+	Com_sprintf( cls.updateChallenge, sizeof( cls.updateChallenge ), "%i", (random() << 16) ^ random());
+
 	Info_SetValueForKey( info, "challenge", cls.updateChallenge );
 	Info_SetValueForKey( info, "renderer", cls.glconfig.renderer_string );
 	Info_SetValueForKey( info, "version", com_version->string );

Modified: trunk/code/qcommon/common.c
===================================================================
--- trunk/code/qcommon/common.c	2009-05-29 03:14:49 UTC (rev 1560)
+++ trunk/code/qcommon/common.c	2009-05-31 19:48:28 UTC (rev 1561)
@@ -2502,6 +2502,22 @@
 
 /*
 =================
+Com_InitRand
+Seed the random number generator, if possible with an OS supplied random seed.
+=================
+*/
+static void Com_InitRand(void)
+{
+	unsigned int seed;
+	
+	if(Sys_Random(&seed, sizeof(seed)))
+		srand(seed);
+	else
+		srand(time(NULL));
+}
+
+/*
+=================
 Com_Init
 =================
 */
@@ -2519,9 +2535,12 @@
 	Com_Memset( &eventQueue[ 0 ], 0, MAX_QUEUED_EVENTS * sizeof( sysEvent_t ) );
 	Com_Memset( &sys_packetReceived[ 0 ], 0, MAX_MSGLEN * sizeof( byte ) );
 
-  // do this before anything else decides to push events
-  Com_InitPushEvent();
+	// initialize the weak pseudo-random number generator for use later.
+	Com_InitRand();
 
+	// do this before anything else decides to push events
+	Com_InitPushEvent();
+
 	Com_InitSmallZoneMemory();
 	Cvar_Init ();
 
@@ -3322,8 +3341,7 @@
 		return;
 
 	Com_Printf( "Com_RandomBytes: using weak randomization\n" );
-	srand( time( 0 ) );
 	for( i = 0; i < len; i++ )
-		string[i] = (unsigned char)( rand() % 255 );
+		string[i] = (unsigned char)( random() % 255 );
 }
 

Modified: trunk/code/renderer/tr_noise.c
===================================================================
--- trunk/code/renderer/tr_noise.c	2009-05-29 03:14:49 UTC (rev 1560)
+++ trunk/code/renderer/tr_noise.c	2009-05-31 19:48:28 UTC (rev 1561)
@@ -44,12 +44,10 @@
 {
 	int i;
 
-	srand( 1001 );
-
 	for ( i = 0; i < NOISE_SIZE; i++ )
 	{
-		s_noise_table[i] = ( float ) ( ( ( rand() / ( float ) RAND_MAX ) * 2.0 - 1.0 ) );
-		s_noise_perm[i] = ( unsigned char ) ( rand() / ( float ) RAND_MAX * 255 );
+		s_noise_table[i] = ( float ) ( ( ( random() / ( float ) RAND_MAX ) * 2.0 - 1.0 ) );
+		s_noise_perm[i] = ( unsigned char ) ( random() / ( float ) RAND_MAX * 255 );
 	}
 }
 

Modified: trunk/code/server/sv_client.c
===================================================================
--- trunk/code/server/sv_client.c	2009-05-29 03:14:49 UTC (rev 1560)
+++ trunk/code/server/sv_client.c	2009-05-31 19:48:28 UTC (rev 1561)
@@ -73,7 +73,7 @@
 		// this is the first time this client has asked for a challenge
 		challenge = &svs.challenges[oldest];
 
-		challenge->challenge = ( (rand() << 16) ^ rand() ) ^ svs.time;
+		challenge->challenge = ( (random() << 16) ^ random() ) ^ svs.time;
 		challenge->adr = from;
 		challenge->firstTime = svs.time;
 		challenge->time = svs.time;

Modified: trunk/code/server/sv_init.c
===================================================================
--- trunk/code/server/sv_init.c	2009-05-29 03:14:49 UTC (rev 1560)
+++ trunk/code/server/sv_init.c	2009-05-31 19:48:28 UTC (rev 1561)
@@ -475,8 +475,7 @@
 	Cvar_Set("cl_paused", "0");
 
 	// get a new checksum feed and restart the file system
-	srand(Com_Milliseconds());
-	sv.checksumFeed = ( ((int) rand() << 16) ^ rand() ) ^ Com_Milliseconds();
+	sv.checksumFeed = ( ((int) random() << 16) ^ random() ) ^ Com_Milliseconds();
 	FS_Restart( sv.checksumFeed );
 
 	CM_LoadMap( va("maps/%s.bsp", server), qfalse, &checksum );



More information about the quake3-commits mailing list