I don’t think you can model it the way you’ve written it, but there’s another formulation that should be equivalent. Rather than trying to use N as a decision variable, you can represent your summation by creating a new binary decision variable associated with each of your random variables. Then you can use those binary variables to encode whether each random variable is included in the summation:

```
using JuMP, Cbc
m = Model(solver=CbcSolver())
# The number of random variables
Nmax = 10
# The random variables themselves
x = randn(Nmax)
# The z variables select which x are included in the sum
@variable m z[1:Nmax] Bin
y = sum(x .* z)
# This constraint means that z must always take the form
# [1, 1, ..., 1, 0, 0, ...., 0]
# which is equivalent to saying y = sum(x[1:N]) for some N
for i in 2:Nmax
@constraint m z[i] <= z[i - 1]
end
@objective m Min y
solve(m)
@show x getvalue(y) getvalue(z)
```

which gives:

```
x = [0.557752, -0.31448, 0.721013, -0.141906, -1.37589, -0.343218, 0.369579, 2.0714, 0.674477, -0.953159]
getvalue(y) = -0.8967336932327254
getvalue(z) = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]
```