# 在 Python 中生成隨機數列表

Najwa Riyaz 2023年1月30日

## 在 Python 中使用 `random.sample()` 函式生成隨機整數

``````random.sample(population_sequence, total_count_of_random_numbers)
``````

``````import random

random_numbers = random.sample(range(6), 3)
print(random_numbers)
type(random_numbers)
``````

``````[3, 0, 1]
list
``````

## 在 Python 中使用 `random.randint()` 函式生成隨機整數

``````random_numbers = [random.randint( < min_value > , < min_value > ) for x in range(total_count_of_random_numbers)]
``````

``````import random

random_numbers = [random.randint(0, 6) for x in range(3)]
print(random_numbers)
``````

``````[1, 6, 6]
``````

## 在 Python 中使用 `numpy.random.randint()` 函式生成隨機整數

``````np.random.randint(low=min_val, high=max_val, size=count).tolist()
``````

``````import numpy as np

random_numbers = np.random.randint(low=0, high=6, size=3).tolist()
print(random_numbers)
``````

``````[2, 4, 2]
``````

## 在 Python 中使用 `numpy.random.uniform()` 函式生成隨機浮點數

``````random.uniform(low=minvalue, high=maxvalue, size=count_of_numbers)
``````

``````import numpy

random_numbers = numpy.random.uniform(low=0, high=6, size=10).tolist()
print(random_numbers)
``````

``````[0.3077335256902074,
4.305975943414238,
4.487914411717991,
1.141532770555624,
2.849062698503963,
3.7662017493968922,
2.822739788956107,
4.5291155985333065,
3.5138714366365296,
3.7069530642450745]
``````