0

I have two lists, high and low bounds, that I would like to use as bounds to create a new list randomly between those values.

LOW HIGH 19.35 45.04 18.09 44.05 17.44 42.07 16.46 41.95 18.38 43.45 20.78 46.04 24.99 49.12 25.22 50.94 24.67 49.32 

I am trying to create a new list that has the length of list but has random values between each LOW and HIGH pair.

A new list might look like:

NEW LIST 38.26 33.70 18.47 41.35 36.77 21.95 25.23 29.18 49.32 

Any help is appreciated, thanks!

So far this is what I have but it doesn't step down a row to update the lower and upper bounds:

Low = pd.Series(data['LOW']).values High = pd.Series(data['HIGH']).values newlist = [] for i in range(0, 8): newlist = random.uniform(Low, High) 
8
  • Are these plain Python lists, or are they Pandas Series or something? Commented Nov 12, 2019 at 23:07
  • These are pd.series Commented Nov 12, 2019 at 23:08
  • That was a typo on my part, fixed Commented Nov 12, 2019 at 23:09
  • Are they inside of a DataFrame? Commented Nov 12, 2019 at 23:09
  • Updated to show initialization of the Low and High, they are not dataframes, just series with no index. Commented Nov 12, 2019 at 23:11

3 Answers 3

1

All hail the power of broadcasting!

import pandas as pd import random import numpy as np # example data lows = pd.Series(np.random.uniform(0, 50, 10)) highs = pd.Series(np.random.uniform(0, 50, 10)) nums_arr = random.uniform(low=lows, high=highs) 

I especially like this solution because it directly creates a Series, no conversions necessary.


A few comments on your code:

1)

Variable names should generally follow the lower_case_with_underscores style. See PEP 8 for more on Python style.

2)

Low = pd.Series(data['LOW']).values High = pd.Series(data['HIGH']).values 

From the rest of the code you showed me, you already know that data is a DataFrame. Getting a column from a DataFrame (e.x: data['LOW']) returns a Series, so the call to pd.Series() is redundant. It also isn't clear why you then call .values().

Bear in mind that using Series.values is discouraged. From the docs:

Warning: We recommend using Series.array or Series.to_numpy(), depending on whether you need a reference to the underlying data or a NumPy array.

3)

newlist = [] for i in range(0, 8): newlist = random.uniform(Low, High) 

I'm really struggling to find the words to explain this one. random.uniform is a function which takes two numbers, and produces a random float between the first two. In this case, however, random.uniform is operating on two Series, thanks to the magic of broadcasting. This is why despite not looking like it makes much sense, the code doesn't actually produce an error or crash.

Notice also that the code above doesn't actually do anything with the result in newlist, nor does newList depend at all on the variable i.

My guess is that you had something like this in mind:

# notice the variable name new_nums_list = [] for i in range(low.size): curr_rand_num = random.uniform(low[i], high[i]) new_nums_list.append(curr_rand_num) 

Which could have also been written:

new_nums_list = [random.uniform(low[i], high[i]) for i in range(low.size)] 

Thanks to the power of zip(), we could do even better:

new_nums_list = [random.uniform(curr_low, curr_high) for curr_low, curr_high in zip(low, high)] 

Any further questions? :)

Sign up to request clarification or add additional context in comments.

1 Comment

This is beyond helpful, appreciate both working code but where I might think about improving my own code. Thank you!
0

I think this should give you the solution you're after. The code below puts the low and high lists into a list of its own, and iterates through each index, randomly picking one.

from random import randint low = [19.35, 18.09, 17.44, 16.46, 18.38, 20.78, 24.99, 25.22, 24.67] high = [45.04, 44.05, 42.07, 41.95, 43.45, 46.04, 49.12, 50.94, 49.32] lists = [low, high] newlist = [] for i in range(0, len(low)): newlist.append(lists[randint(0, 1)][i]) 

This is an output I just generated, for reference:

[19.35, 18.09, 42.07, 16.46, 18.38, 46.04, 24.99, 25.22, 49.32]

1 Comment

I believe OP wants to generate a random number in the range [a, b], not choose one of a or b.
0
import random HIGH = [45.04, 44.05, 42.07, 41.95, 43.45, 46.04, 49.12, 50.94, 49.32] LOW = [19.35, 18.09, 17.44, 16.46, 18.38, 20.78, 24.99, 25.22, 24.67] result = [] for low,high in zip(LOW,HIGH): result.append(random.uniform(low,high)) print(result) 

Example output:

[20.23415150413731, 31.21737764031939, 33.39330466412157, 29.23878916462911, 33.23211876295469, 35.20524920370517, 32.21931456613152, 44.07717212042314, 48.56798614652851] 

2 Comments

Why call str before printing the result?
No list comprehension either?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.