Results 1 to 9 of 9

Thread: [RESOLVED] Normalized range

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    121

    Resolved [RESOLVED] Normalized range

    I've got two numbers, A and B. I don't know which is bigger at the outset. I'm trying to work out a math function that will use the two numbers (without IF statements) and end in a result from 0 to 2, with 1 being the answer if A = B. If at all possible, it'd be awesome if this followed a 'normal distribution' curve (bell curve).
    This is probably staring me in the face or something, but right now I'm blind to it.

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Normalized range

    Your question is a bit vague, but maybe 2exp(-(A-B)^2) is what you're after. It's 2 when A=B and is normally distributed in some sense. If you have more specific requirements, maybe those can be added in. For instance, you could tweak the standard deviation.
    Last edited by jemidiah; Nov 4th, 2014 at 03:43 AM.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    121

    Re: Normalized range

    Alright, maybe I don't know how to program that one, but it didn't seem to work. Test values of A and B got results well outside of the 0 to 4 range. As I understood the above, the code would be 2 * EXP((-1*(A-B))^2).
    Trying again, somewhat more formal:
    A and B are integers from 1 to positive infinity. With a function f, it would have the following properties:
    f(A,B) = 1 when A = B
    f(A,B) >= 0 for all A and B
    f(A,B) <= 2 for all A and B

    The point: I'm working on a 'chance to hit' number. With rising levels, the 'raw skill' of attack and defense will always be above 0 but I can't place any known explicit limit on an upper end. I'm hoping to avoid the IF statement in my code by comparing attack and defense in some equation and coming up with a chance to hit that is between two points (specifically 50% to 100%) with low proportional differences mattering little (hence the curve if possible). Something that would look like:

    If Int(Rnd()*100)+1 > f(A,B) Then
    'Insert code for hitting the bad guy here.
    End If

  4. #4
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Normalized range

    You put the negative inside the parens when it should be outside, i.e. 2 * EXP(-1*(A-B)^2). For instance, if A=1, B=0, you should get 2/e = 0.735.... Without the negative the function grows very quickly for |A-B| large; with it, it decreases very quickly. It sounds like this is what you want for f, though you might need to tune it to "drop off" more or less quickly depending on your needs, which aside from normalization amounts to changing the variance.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    121

    Re: Normalized range

    Ah. Redid the equation. Okay, I messed up the definition. My bad. Trying again.
    f(A,B) = 1 when A = B
    f(A,B) approaches 2 when A > B
    f(A,B) approaches 0 when B > A
    I was thinking this might be something based on a sin function, but haven't worked it out yet.
    The equation provided gives numbers from 0 to 2 where f(A,B) = 2 when A = B.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    121

    Re: Normalized range

    Worked it out.
    f(a,b) = Tan((((a - b) / Max(a, b)) * 45 * PI) / 180) + 1
    Generates a number between 0 and 2, f(a,b) is 1 when a = b, approaches 0 when B > A, and approaches 2 when A > B.
    Just need to check the curvature of the equation.

  7. #7
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Normalized range

    Glad it worked out. For your revised problem, I would probably have gone for arctan, something like arctan(A-B)*2/pi + 1. It's 1 at A=B, pi/2*2/pi + 1 = 2 at A >> B, and -pi/2*2/pi + 1 = 0 at A << B.
    Last edited by jemidiah; Nov 5th, 2014 at 03:55 AM.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    121

    Re: Normalized range

    Yours is much better, and less complicated. Thank you so much! I can take that, cube it (or more) to fix the curve (so that values where A is close to B are near 1).

  9. #9
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Normalized range

    Sure, there's a lot of ways to tweak it as you wish. Divide (A-B) by a magic constant, replace (A-B) with sign(A-B)*|A-B|^x for some constant x (for x=3 this reduces to just (A-B)^3; for x=2 you need the sign(A-B) bit), replace arctan with another function of the same ilk like the "logistic function" or well-enough-behaved composites of either of these like sin(arctan(A-B)) = (A-B)/sqrt((A-B)^2 + 1), etc. Just depends on what you're after. Good luck.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width