# math helpers

all of these live under the `Math` namespace

```csharp
Math.abs(-5) // 5
Math.min(3, 7) // 3
Math.max(3, 7) // 7
Math.clamp(15, 0, 10) // 10: restricts a value to a range
Math.parseNumber("42.5") // 42.5 (parses string to number)
Math.floor(4.7) // 4
Math.ceil(4.2) // 5
Math.round(4.5) // 5
Math.randomInt(1, 6) // a random whole number from 1 to 6
Math.randomDecimal(0.0, 1.0) // a random decimal from 0.0 to 1.0
Math.sqrt(4) // 2
Math.sin(60) // ~0.866
Math.cos(60) // ~0.5
```

### `Math.clamp`

```csharp
var roundDelay = 5

// each round, shrink the delay but never let it go below 1 second
roundDelay = Math.max(1, roundDelay - 0.25)
```

or with `Math.clamp` if you also want an upper bound:

```csharp
roundDelay = Math.clamp(roundDelay - 0.25, 1, 5) // never below 1, never above 5
```

### doing your own random pick without `Katton.pick`

if you just need a random index into a list rather than a random element (to also know *which* index you picked):

```csharp
val colors = ["red", "blue", "lime"]
val index = Math.randomInt(0, colors.size() - 1)
val chosen = colors[index]
```

...though for the common case of "just give me a random element," [Katton.pick](../api/katton-pick.md) is simpler and does exactly this for you! :D
