Staredit Network > Forums > Technology & Computers > Topic: Random Function Simplification
Random Function Simplification
Apr 26 2009, 9:32 pm
By: Falkoner  

Apr 26 2009, 9:32 pm Falkoner Post #1



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:

Code
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.

Apr 26 2009, 10:10 pm WoodenFire Post #2



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.

Apr 26 2009, 10:14 pm Falkoner Post #3



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.

Apr 27 2009, 11:45 am ShadowFlare Post #4



Code
   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:

Code
   return ( rand() % (high - low + 1) ) + low;

This reduces the function to:

Code
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.

Apr 28 2009, 2:12 am Falkoner Post #5



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.

Apr 30 2009, 3:58 am ShadowFlare Post #6



Are you talking about the modulus operator? That is a built-in operator; no header file is needed for it.



None.

Apr 30 2009, 4:33 am Falkoner Post #7



Yeah, I was thinking of modulus with floating point integers, you need math.h for that.



None.

May 3 2009, 3:43 am Doodle77 Post #8



This does not produce appropriately random numbers, see http://www.azillionmonkeys.com/qed/random.html .
I reccomend the approach under "Another Alternative".



None.

May 3 2009, 6:16 am Falkoner Post #9



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.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[01:19 pm]
Vrael -- IM GONNA MANUFACTURE SOME SPORTBALL EQUIPMENT WHERE THE SUN DONT SHINE BOY
[2024-5-02. : 1:35 am]
Ultraviolet -- Vrael
Vrael shouted: NEED SOME SPORTBALL> WE GOT YOUR SPORTBALL EQUIPMENT MANUFACTURING
Gonna put deez sportballs in your mouth
[2024-5-01. : 1:24 pm]
Vrael -- NEED SOME SPORTBALL> WE GOT YOUR SPORTBALL EQUIPMENT MANUFACTURING
[2024-4-30. : 5:08 pm]
Oh_Man -- https://youtu.be/lGxUOgfmUCQ
[2024-4-30. : 7:43 am]
NudeRaider -- Vrael
Vrael shouted: if you're gonna link that shit at least link some quality shit: https://www.youtube.com/watch?v=uUV3KvnvT-w
Yeah I'm not a big fan of Westernhagen either, Fanta vier much better! But they didn't drop the lyrics that fit the situation. Farty: Ich bin wieder hier; nobody: in meinem Revier; Me: war nie wirklich weg
[2024-4-29. : 6:36 pm]
RIVE -- Nah, I'm still on Orange Box.
[2024-4-29. : 4:36 pm]
Oh_Man -- anyone play Outside the Box yet? it was a fun time
[2024-4-29. : 12:52 pm]
Vrael -- if you're gonna link that shit at least link some quality shit: https://www.youtube.com/watch?v=uUV3KvnvT-w
[2024-4-29. : 11:17 am]
Zycorax -- :wob:
[2024-4-27. : 9:38 pm]
NudeRaider -- Ultraviolet
Ultraviolet shouted: NudeRaider sing it brother
trust me, you don't wanna hear that. I defer that to the pros.
Please log in to shout.


Members Online: Roy