(Sorry, this post has gotten long. I realise now how many comments of mine are useless)
This is honestly something I submitted as part of a group (non-final) games project. 25 times a second, the update is called and this function is called as part of that. I considered taking out the comments that say "42% chance" because they make it too readable.
// Forecast: Sunny, with a chance of bad guys.
// But not too many bad guys.
void CGameApplication::randomSpawn(float timeValue)
staticint cooldownCounter; // minimum number of calls between spawns
if (cooldownCounter > 0)
// if no enemies, spawn one (for the hell of it)
if (m_hostileCount == 0)
// is this overkill? hell yeah. is it slow? hell yeah. does it work? hell knows.
float rnd1 = (rand()%10001) * 0.0001f; // four
float rnd2 = (rand()%10001) * 0.0001f; // randoms
float rnd3 = (rand()%10001) * 0.0001f; // from
float rnd4 = (rand()%10001) * 0.0001f; // 0 to 1
bool blue_moon = (rnd1 > 0.58); // 42% chance
bool pink_elephants_fly = ((int)(rnd2 * 100) % 3 == 0); // if rounded random number 1-100 is a multiple of 3; 33% chance
bool snowing_in_july = (rnd3 * timeValue) > 0.08; // approx 50%, more if the game is running slow, 0 if it runs too fast
int power_level = (rnd4 < 0.2) ? 9001 : 8999; // 20% chance
if ((blue_moon && pink_elephants_fly) || (power_level > 9000 && snowing_in_july)) // about 14% chance of spawn
if (m_hostileCount < c_hostileMax)
if (rand() % 2 == 0)
cooldownCounter = 200; // frames between spawns, 100 is about 1.5 seconds depending on framerate
cooldownCounter += 4; // minimum frames between attempts at spawning, regardless of whether anything spawns or not
Yes, this code works. And I don't think it's the worst code in the project, FYI. There were (honestly) the following declarations in the code:
int *xPos= newint;
int *yPos= newint;
Oh, looking through the source I also found this gem.
if (abs(pos.x) > m_spaceSize.x || abs(pos.y) > m_spaceSize.y || abs(pos.z) > m_spaceSize.z)
// object has transcended the space-time continuum (actually just really far away)
Edit: Heck, I'm on a roll and back for more. These are from a C# (non-education-related) project.
///<summary>/// Yes, this is little more than a List<FileInfo>, but so what?
///</summary>internal class FileBucket
public FileBucket(byte MaxFiles)
if (MaxFiles == byte.MaxValue)
MaxFiles--; // noone will notice
this.MaxFiles = MaxFiles;
internal enum AnimMode
None, // don't worry (maybe i should; this mode makes no sense)
SlideIn, // whole slide appearing
SlideVisible, // whole slide is visible
return; // the "SCREW YOU ALL ILL FIX IT SOON" response
I use jokes in the comments (the workaround comments are generally very ironic), and in a reviewed code when commenting what's wrong with that particular piece. Because it's marked TODO, and to be soon fixed by a recipient. But I slip an occasional 'obviously' (as in 'this code does that.. obviously) or 'should be doing' variations.
List<decimal> fixedDenominations = ...
double fixedDenArray = newdouble[fixedDenominations.Count];
int i = 0;
//this is a very nice way to do this, but I choose not to use it because I want that the person
//who reads my code can understand what I'm trying to do
//fixedDenominations.ForEach(d => fixedDenArray[i++] = (double)d);
foreach (decimal d in fixedDenominations)
fixedDenArray[i++] = (double)d;
I remember the time I had started my career in a small company, when no source control exists. We use to back up the folders everyday as old, new, _Date etc. Once it reached its height when a back up folder was created as "latestold" (???)
The only time I ever attempted to add a joke as a comment was in an attempt to hide the embarrassment of using a 'GOTO' statement in some vba code was writing (about 100 years ago); can't remember the joke!
for the record: That was the first and last time I ever used a GOTO statement
Last Visit: 31-Dec-99 19:00 Last Update: 31-Jan-15 4:19