more complex and effeciant random function

Talk about making games.

more complex and effeciant random function

Postby Game A Gogo » Wed Jan 23, 2008 12:22 am

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!!!
Last edited by Game A Gogo on Wed Jan 23, 2008 12:25 am, edited 1 time in total.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: more complex and effeciant random function

Postby DarkParadox » Wed Jan 23, 2008 12:24 am

thanks game a gogo
User avatar
DarkParadox
 
Posts: 457
Joined: Mon Jan 08, 2007 11:32 pm
Location: USA, Florida.
Score: 84 Give a positive score

Re: more complex and effeciant random function

Postby Fuzzy » Wed Jan 23, 2008 2:07 am

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.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: more complex and effeciant random function

Postby Game A Gogo » Wed Jan 23, 2008 2:41 am

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
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: more complex and effeciant random function

Postby Fuzzy » Wed Jan 23, 2008 2:50 am

good work gogo.. but you can make that shorter still! And more obfuscated..
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: more complex and effeciant random function

Postby pyrometal » Wed Jan 23, 2008 4:37 am

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
SPRITE WARRIOR:
Free, open-source & crossplatform pixel art editor (currently under construction).
User avatar
pyrometal
 
Posts: 706
Joined: Wed Nov 28, 2007 4:07 am
Location: Kingston, ON, Canada
Score: 86 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron