user2829468 user2829468 - 6 months ago 12
Python Question

How to make a random array in Python?

Sorry for the noob question.. But I can't seem to figure out how to make an array with a random set of values.

My goal is to make an array with a set of 10 random numbers (like [10, 2, 45, 22, 31, 22, 12, 88, 90, 6]) Does anyone know how I can do this in python?

Answer

Using the random module:

>>> import random
>>> L = range(100)
>>> amount = 10
>>> [random.choice(L) for _ in range(amount)]
[31, 91, 52, 18, 92, 17, 70, 97, 17, 56]