Okay, well here's a function that I use a lot that I think that I've simplified as much as I can, but I wanna be certain, since I use it a lot, it's a random function:
int random(int low_number, int high_number) //Generates a random number from low to high
{//(requires stdlib.h and time.h)
int low, high;
if(low_number > high_number){low = high_number; high = low_number;}
if(high_number > low_number){high = high_number; low = low_number;}
if(high_number == low_number){return high_number;}
int random, range;
if(low < 0 && high < 0){range = (low * -1) - (high * -1) + 1;}
if(low < 0 && high >= 0){range = high + (low * -1) + 1;}
if(low >= 0 && high < 0){range = (high * -1) + low + 1;}
if(low >= 0 && high >= 0){range = high - low + 1;}
random = ((rand() % range)); //Generates a number, between low and the high number range
return random + low; //Adds low to number, also allows negatives
}
Basically, while it says low to high on the argument list, this function will automatically find which is low or high anyway, so they are actually interchangeable.
So basically, I'm wondering if there's anyway I could simplify this function to either use less memory or less processing power but fulfill all the same requirements which are:
-Interchangeable high and low input
-Can do both negative and positive numbers, even combined
-Can stand-alone as a function with only those 2 header files.
None.
I believe math.h has a function for this already and the way it optimizes the low and high is by making Low always 0 and for you to make the low range, u simply add a number to the random command...
(rand() %30)+10, will give numbers from 10 to 40...
And any addition optimizations could make that even faster perhaps.
Make sure you list if statements in order of likelyhood that there conditions will be met...
Always use ++H instead of H=H+1 or --H instead of H=H-1
You could also use other systems that have numbers and just call that number up and reduce/expand it to create a random number faster...
those are some quick little ideas you prolly already figured out
None.
Well, admittedly, math.h would easily reduce the code, since I could use modulus to find the range, however, I often make programs that don't need math.h, but use the randomization, so I didn't want to have to add a third header file.
None.
if(low < 0 && high < 0){range = (low * -1) - (high * -1) + 1;}
if(low < 0 && high >= 0){range = high + (low * -1) + 1;}
if(low >= 0 && high < 0){range = (high * -1) + low + 1;}
if(low >= 0 && high >= 0){range = high - low + 1;}
Because of your previous conditions, this can be reduced to:
range = high - low + 1;
Since there is no condition needed, you could actually combine everything from "int random, range;" to "return random + low;" to just this line:
return ( rand() % (high - low + 1) ) + low;
This reduces the function to:
int random(int low_number, int high_number) //Generates a random number from low to high
{//(requires stdlib.h and time.h)
int low, high;
if(low_number > high_number){low = high_number; high = low_number;}
if(high_number > low_number){high = high_number; low = low_number;}
if(high_number == low_number){return high_number;}
return ( rand() % (high - low + 1) ) + low; //Generates a number, between low and the high number range
}
Post has been edited 2 time(s), last time on Apr 27 2009, 11:51 am by ShadowFlare.
None.
Wait a minute, is modulus included in stdlib or time.h? Because I forgot that I have to use it with the rand() function.. Dang. Ah well, anyways, I can't believe I didn't think of that, thanks SF, in my programming class I asked the same question, 'cuz I coulda sworn I had something like that previously, but when I revised it it didn't seem to work in my head, it does though, thanks
Less variables too!
None.
Are you talking about the modulus operator? That is a built-in operator; no header file is needed for it.
None.
Yeah, I was thinking of modulus with floating point integers, you need math.h for that.
None.
This does not produce appropriately random numbers, see
http://www.azillionmonkeys.com/qed/random.html .
I reccomend the approach under "Another Alternative".
None.
Uh, upon testing it produces the variables as random as probability says they should be, and I honestly could care less about it being "actually random" because it works for just about anything imaginable, and anyone who honestly cares about it not being perfect needs to ease up a bit, because this is simple, and it's effective.
None.