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.
[08:09 am]
Zycorax -- Greed
[03:25 am]
Symmetry -- why do the oligarchs want our mapping strategies???
[03:24 am]
Symmetry -- the question is why though
[01:10 am]
Butch -- made me feel really sad and stupid and fat
[01:10 am]
Butch -- I got duped by one and replied and then realized after
[11:26 pm]
Sie_Sayoka -- the turings have tested
[03:07 pm]
UndeadStar -- https://staredit.net/topic/18961/ <= wow, if there wasn't a capital letter on almost every word in the title, and a link in the signature, I may not have guesses it's an AI bot. I don't know much about map editing, but the content may even be relevant enough to be kept.
[2026-11-10. : 4:16 am]
lil-Inferno -- fart screen
[2026-11-09. : 3:49 pm]
NudeRaider -- Ultraviolet
Ultraviolet shouted: :wob: maybe not replaced, just add a discord applet below the shoutbox imo
split screen. Left side shoutbox, right side discord meme channel
[2026-11-08. : 10:23 pm]
Ultraviolet -- :wob: maybe not replaced, just add a discord applet below the shoutbox imo
Please log in to shout.


Members Online: ihjel