bell shaped random function
Displaying 1-2 of 2 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
gannon

I made a little function that gives you a random number with the distribution shaped like a bell. (The numbers in the middle are more likly)

int randomBell(int a,int b)

{
if(a < b)
return (Random(a/2,b/2)+Random((a/2) + (a%2),(b/2) + (b%2)));
return (Random(b/2,a/2)+Random((b/2) + (b%2),(a/2) + (a%2)));
}

Posted on 2005-02-11 16:50:24

gannon

Sorry for double posting but I made a more advanced version.
Just increase type to have it have a skinnier bell. 2 is the same as the old random bell.

int randomBell(int a,int b, int type)

{
int result;
int offset;
int multi = 1;
if(a > b)
{
result = b;
b = a;
a = result;
}
if(a < 0)
{
if(b < 0)
{
result = b;
b = a * (0-1);
a = result * (0-1);
multi = 0-1;
}
else
{
offset = a;
a = 0;
b = b - offset;
}
}

result = 0;
int i;
int a_odd = a%type;
int b_odd = b%type;

for(i = 0; i =< type; i++)
{
result += Random(a/type + lessthan(i,a_odd),b/type + lessthan(i,b_odd));
}
return (result + offset * multi);

}

int lessthan(int a, int b)
{
if(a < b)
{
return 1;
}
return 0;
}


now works with neg numbers :)

Posted on 2005-02-11 19:04:01 (last edited on 2005-02-11 22:27:35)


Displaying 1-2 of 2 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.