Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clamp a value from 0 to infinite to a value from 0 to 1?

I have a program in Java that generates a float value aggressiveness that can be from 0 to infinite. What I need to do is that the higher this float is, the higher there are chances the program fires a function attackPawn().

I already found out that I need the function Math.random(), which gives a random value between 0 and 1. If Math.random() is lower than aggressiveness transformed into a float between 0 and 1, I call the function attackPawn().

But now I am stuck, I can't figure out how I can transform aggressiveness from 0 to infinite to a float which is from 0 to 1, 1 meaning "infinite" aggressiveness and 0 meaning absence of anger.

Any ideas or math equation?

like image 792
turbodoom Avatar asked Apr 16 '14 09:04

turbodoom


People also ask

What does it mean to clamp a value?

In computer science, we use the word clamp as a way to restrict a number between two other numbers. When clamped, a number will either keep its own value if living in the range imposed by the two other values, take the lower value if initially lower than it, or the higher one if initially higher than it.

How do you clamp a value in Python?

clip() function is used to Clip (limit) the values in an array. Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.


1 Answers

You want a monotonic function that maps [0...infinity] to [0..1]. There are many options:

    y=Math.atan(x)/(Math.PI/2);
    y=x/(1+x);
    y=1-Math.exp(-x);

There are more. And each of those functions can be scaled arbitrarily, given a positive constant k>0:

    y=Math.atan(k*x)/(Math.PI/2);
    y=x/(k+x);
    y=1-Math.exp(-k*x);

There is an infinite number of options. Just pick one that suits your needs.

like image 177
pentadecagon Avatar answered Sep 19 '22 18:09

pentadecagon