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.
[2026-6-04. : 3:42 am]
RIVE -- Hellooooo, Nurse
[2026-6-03. : 4:19 pm]
NudeRaider -- Hello World!
[2026-6-01. : 4:03 pm]
IskatuMesk -- no Hello World? No Hello . jpeg? Not even ddosing some government website? smh my head
[2026-5-31. : 10:02 pm]
Symmetry -- I was really just figuring out how to compile
[2026-5-31. : 10:02 pm]
Symmetry -- I didn't make it do anything
[2026-5-31. : 9:17 pm]
Ultraviolet -- hell yeah. did you have trouble making the button functional, or just haven't gotten there yet?
[2026-5-31. : 8:29 pm]
Symmetry -- I made the marine shoot lasers and gave the medic a nonfunctional button
[2026-5-31. : 8:07 pm]
Ultraviolet -- Symmetry
Symmetry shouted: I did my first EUD thing today. Feels like me finding arsenal at age 8 again
haha for sure. in some ways it's even more exciting because you can play EUD maps on regular battle.net, don't need to convince someone to download your mod to play with you. what did you do?
[2026-5-31. : 6:47 pm]
Symmetry -- I did my first EUD thing today. Feels like me finding arsenal at age 8 again
[2026-5-31. : 3:20 pm]
l)ark_ssj9kevin -- le reddit
Please log in to shout.


Members Online: Roy