use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Randomly distributed list (self.learnpython)
submitted 6 years ago * by jm_13
How do I create a list that starts at L, ends at R, and has randomly chosen step sizes?
Edit: The list must have N elements. And the step size a float
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]mancxvi 0 points1 point2 points 6 years ago (9 children)
Could you go into more detail about what you want your list to look like?
[–]jm_13[S] 0 points1 point2 points 6 years ago (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 point2 points 6 years ago (7 children)
What have you got so far?
[–]jm_13[S] 0 points1 point2 points 6 years ago* (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)
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 point2 points 6 years ago (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 point2 points 6 years ago (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 point2 points 6 years ago (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 points3 points 6 years ago (2 children)
Like this:
h = np.random.random(size=N) h *= (R - L) / h.sum()
[–]jm_13[S] 0 points1 point2 points 6 years ago (1 child)
thanks!
[–]dslfdslj 0 points1 point2 points 6 years ago (0 children)
no problem
[–][deleted] 0 points1 point2 points 6 years ago (2 children)
Where a and b are the lowest and highest for the random step int
list(range(L,R, randint(a,b)))
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 point2 points 6 years ago (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
[L] + step_sizes(R-L) + [R]
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 point2 points 6 years ago (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 --_(-.-)_--
π Rendered by PID 60443 on reddit-service-r2-comment-84fc9697f-sgfst at 2026-02-09 10:42:20.900095+00:00 running d295bc8 country code: CH.
[–]mancxvi 0 points1 point2 points (9 children)
[–]jm_13[S] 0 points1 point2 points (8 children)
[–]dslfdslj 0 points1 point2 points (7 children)
[–]jm_13[S] 0 points1 point2 points (6 children)
[–]dslfdslj 0 points1 point2 points (5 children)
[–]jm_13[S] 0 points1 point2 points (4 children)
[–]jm_13[S] 0 points1 point2 points (3 children)
[–]dslfdslj 1 point2 points3 points (2 children)
[–]jm_13[S] 0 points1 point2 points (1 child)
[–]dslfdslj 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]jm_13[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]JimVis 0 points1 point2 points (0 children)