简体   繁体   中英

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?

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]

Here is how you can create the array

import random

ar=random.sample(range(100),20)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM