Skip to content

Instantly share code, notes, and snippets.

@Eeman1113
Created May 22, 2024 20:42
Show Gist options
  • Save Eeman1113/7a20c7b67315eac6bfde1666845ae675 to your computer and use it in GitHub Desktop.
Save Eeman1113/7a20c7b67315eac6bfde1666845ae675 to your computer and use it in GitHub Desktop.
def generate_random_list(shape):
"""
Generate a list with random numbers and shape 'shape'
[4, 2] --> [[rand1, rand2], [rand3, rand4], [rand5, rand6], [rand7, rand8]]
"""
if len(shape) == 0:
return []
else:
inner_shape = shape[1:]
if len(inner_shape) == 0:
return [random.uniform(-1, 1) for _ in range(shape[0])]
else:
return [generate_random_list(inner_shape) for _ in range(shape[0])]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment