![](/img/trans.png)
[英]Pseudo-randomly pick an element from a list only if it was not chosen yet
[英]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.