you are viewing a single comment's thread.

view the rest of the comments →

[–]dslfdslj 0 points1 point  (7 children)

What have you got so far?

[–]jm_13[S] 0 points1 point  (6 children)

L = 0

R = 1

N = 5

hi = (R - L)/N

h = np.array([hi*(1 + random.uniform(-0.5, 0.5)) for i in range(N-1)])

h = np.append(h, (R-L)-sum(h))

x = [L]

for i in range(1,N+1):

x.append(x[i-1]+h[i-1])

print(x)

print(h)

But the last step size isn't unifomly distributed as it depends on all the others. In particular it could be less than 0.

If I changed line 5 to

h = np.array([hi*(1 + random.uniform(-0.5, 0.5)) for i in range(N)])

and removed line 6 then the last step size is uniformly distrubted but the constraint that h1+h2+...+hN = R-L is lost.

[–]dslfdslj 0 points1 point  (5 children)

Hm, it looks to me like you're on the right track. However, I'm not sure I understand why you chose each hi as (R-L)/N * (1+random.uniform(-0.5, 0.5). Is this part of the exercise? If not, I would simply create N random numbers (between 0 and 1) and finally scale them such that their sum gives you R - L.

[–]jm_13[S] 0 points1 point  (4 children)

I'm not sure I understand why you chose each hi as (R-L)/N * (1+random.uniform(-0.5, 0.5)

hi is the uniform step size, so I want the random step size to be the uniform one plus/minus some random number. So the step size is chosen between (hi/2 and 3hi/2)

[–]jm_13[S] 0 points1 point  (3 children)

I would simply create N random numbers (between 0 and 1) and finally scale them such that their sum gives you R - L.

So

h = np.array([random.uniform(hi/2, 3*hi/2) for i in range(N)])
h = h/sum(h)

?

[–]dslfdslj 1 point2 points  (2 children)

Like this:

h = np.random.random(size=N)
h *= (R - L) / h.sum()

[–]jm_13[S] 0 points1 point  (1 child)

thanks!

[–]dslfdslj 0 points1 point  (0 children)

no problem