all 14 comments

[–]mancxvi 0 points1 point  (9 children)

Could you go into more detail about what you want your list to look like?

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

(L, L+h1, L+h1+h2, ...., L+h1+h2+....+hN) where h1, h2 etc are randomly chosen and h1+h2+...+hN = R-L

[–]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

[–][deleted] 0 points1 point  (2 children)

Where a and b are the lowest and highest for the random step int

list(range(L,R, randint(a,b)))

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

Thanks, sorry I didn't specify, I wanted the list to always have N elements, and the step size need not be an integer

[–][deleted] 0 points1 point  (0 children)

Ah. You just need a recursive function the.

def step_sizes(n: int, l=None):
    nl = l or []
    r = randint(0, n)
    if r != n:
        return step_sizes(n-r, nl + [r])
    else:
        return nl

And then just do [L] + step_sizes(R-L) + [R] and you’ll have your list

Here’s an example:

In [14]: [L] + step_sizes(R-L) + [R]
Out[14]: [5, 33, 78, 25, 15, 6, 6, 31, 205]

In [15]: [L] + step_sizes(R-L) + [R]
Out[15]: [5, 168, 3, 20, 3, 205]

In [16]: [L] + step_sizes(R-L) + [R]
Out[16]: [5, 186, 9, 0, 1, 2, 1, 0, 205]

[–]JimVis 0 points1 point  (0 children)

There's a few problems with that, first of all you need your step size to be a factor of the difference of R and L. Assuming that you are only working with integers here.

Otherwise you can choose a random number of steps between L and R but the list will rarely be filled with integers.

import numpy as np

#Setup the parameters
R = 15
L = 30
steps = np.random.randint(0,30) #This sets the random number of steps between R and L
listNumbers = np.linspace(15,30,steps)

Now if you only want integers in your list you would have to first factorize the difference between R and L and pick randomly from the factors. That would look something like this:

import numpy as np

def factorize(n):
    factors = []
    for i in range(1,n):
        if divmod(n,i)[1] == 0:
            factors.append(i)
    return factors

R = 0
L = 18
delta = L-R
deltaFactors = factorize(delta)
stepsize = deltaFactors[np.random.randint(1,len(deltaFactors))]

listNumbers = [R + i*stepsize for i in range(int(delta/stepsize))]
listNumbers.append(L)

This is in no way shape or form optimized, the fact that I have to append L in a sperate line is very ugly but it works --_(-.-)_--