Page 1 of 1

more complex and effeciant random function

PostPosted: Wed Jan 23, 2008 12:22 am
by Game A Gogo
Code: Select all
int random(int maxi, int mini)//This code was made by Game-a-Gogo a.k.a. Frederic Duguay
{
    int range=maxi-mini;
    if(range<0)
    {
        return 0;//Means that mini is higher then maxi
    }
    else
    {
        return rand(range)+mini;
    }
}


then you could use it like: random(100,10); so it would give out a number between 100 and 10, this also works for negative.
Maybe I should also make one for double

NOTE: PUT THIS IN THE GLOBAL CODE!!!

Re: more complex and effeciant random function

PostPosted: Wed Jan 23, 2008 12:24 am
by DarkParadox
thanks game a gogo

Re: more complex and effeciant random function

PostPosted: Wed Jan 23, 2008 2:07 am
by Fuzzy
A dubious improvement...
Code: Select all
int random(int maxi, int mini)//This code was made by Game-a-Gogo a.k.a. Frederic Duguay Modified and without permission by Fuzzy
{
    int range; // we will use this for two purposes
    range =  min(mini, maxi); // temporarily hold the smaller var in range.
    maxi = max(mini, maxi); // set maxi to the higher between mini and maxi.. error correction line
    mini = range; // correct the value of mini with stored minimum number more error correction
    range = maxi-mini; // now we dont have to check. its doubtful if this would be faster, but it doesnt halt the function
    return rand(range)+mini;
}


Thanks to Gogo for a great function. Its certainly possible to improve it from what I have done.. can you do it? Lets see what you got. I'll show you the tightest version after you try to beat my sample. @ points to whoever can improve on mine.. The clues are all there.

Re: more complex and effeciant random function

PostPosted: Wed Jan 23, 2008 2:41 am
by Game A Gogo
oh yeah, never tough about that! but here is what is better :3
Code: Select all
int random(int maxi, int mini)//This code was made by Game-a-Gogo a.k.a. Frederic Duguay Modified and without permission by Fuzzy
{
    int range=max(maxi,mini)-min(maxi,mini);
    return rand(range)+min(maxi,mini);
}


:3

way shorter, but does the same thing ;3

Re: more complex and effeciant random function

PostPosted: Wed Jan 23, 2008 2:50 am
by Fuzzy
good work gogo.. but you can make that shorter still! And more obfuscated..

Re: more complex and effeciant random function

PostPosted: Wed Jan 23, 2008 4:37 am
by pyrometal
Well, wouldn't be the best way?

Code: Select all
int random(int boundary1, int boundary2) //Pyrometal's version
{
    int A = boundary2 - boundary1;    //Range
    return boundary1 + rand(A+(A/abs(A)));   //(A/abs(A)) determines the need to add or substract 1 to correct the range.
}


The only problem is that if boundary2 is larger than boundary1, boundary1's value becomes exclusive to the range of random numbers.

-- Pyro