# How do I stop a cycle time from going negative?

A normal cycle time has no floor, so a draw can fall below zero when the spread is large. The engine cuts such a draw to zero and warns. BoundedNormal keeps every draw between a minimum and a maximum instead. The script runs one cell with each.

```python
from nagasim import build as nb
from nagasim.core.distributions import distribution_mean


def cell_run(cycle):
    m = nb.Model(time_limit=4000, warmup=0)
    m.add_resource(name="Cell")
    parts = m.create(distribution=nb.Exponential(rate=0.4))
    parts >> m.process(resource_name="Cell", distribution=cycle) >> m.dispose()
    return m.run(replications=10, seed=1).kpi("resource/Cell/Time in Queue Avg")


# Mean 2 minutes, sd 1. A Normal can draw a negative time, which the engine cuts to 0 with a warning.
# BoundedNormal keeps every draw between 0.5 and 4 minutes, which also shifts its mean a little.
normal = nb.Normal(mean=2, stddev=1)
bounded = nb.BoundedNormal(mean=2, stddev=1, min=0.5, max=4)
print(f"Normal(2, 1)                 mean wait {cell_run(normal):.2f} minutes")
print(f"BoundedNormal(2, 1, 0.5..4)  mean wait {cell_run(bounded):.2f} minutes, "
      f"mean time {distribution_mean('BoundedNormal', bounded.params):.2f} minutes")

```

Save it as `recipe-bounded-cycle.py` and run it with `python`. The script seeds itself, so the numbers below are the same on your machine.

```text
Normal(2, 1)                 mean wait 4.70 minutes
BoundedNormal(2, 1, 0.5..4)  mean wait 5.78 minutes, mean time 2.08 minutes

```

## How it works

1. `Normal(mean, stddev)` can draw a negative time, which the run cuts to 0 with a warning.
2. `BoundedNormal(mean, stddev, min, max)` keeps each draw inside the limits, which also moves its mean, here from 2.00 to 2.08 minutes.
3. The two cells wait differently, so choose the bound that matches the physical limit and not the one that looks tidy.

## Elements used

- Distributions: [Normal](../../reference/distributions/normal.md), [BoundedNormal](../../reference/distributions/bounded-normal.md), [Exponential](../../reference/distributions/exponential.md).
- Blocks: [CREATE](../../reference/blocks/create.md), [PROCESS](../../reference/blocks/process.md), [DISPOSE](../../reference/blocks/dispose.md).
- Python: [`Model`](../../reference/api/Model.md).

Back to all [recipes](index.md).
