簡體   English   中英

從列表中隨機選擇元素,均勻分布,無需替換

[英]pick elements from the list randomly in uniform distribution without replacement

我有一個范圍從 0 到 30 的列表

arr = range(0,30)

我需要使用均勻分布從列表中選擇一個“m”個元素樣本,無需替換。 我使用了 random.uniform() ,它給出了浮點數的隨機值。

誰能告訴我如何使用均勻分布從給定列表中隨機選擇“m”個元素而不進行替換?

您可以使用random.sample進行采樣而無需更換

# Python3 program to demonstrate
# the use of sample() function

# import random
from random import sample

# Prints list of random items of given length
arr = range(0,30)

m=5

mysamp = sample(arr,m)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM