我試圖創建1到1000之間的10,000個隨機數的列表。但是我希望80-85%的數字是相同的類別(我的意思是大約100個數字其中應該出現在隨機數列表中的80%的時間),其餘的出現在大約15-20%的時間。任何想法,如果這可以在Python/NumPy/SciPy中完成。謝謝。Python中的加權隨機數值列表
1
A
回答
1
這裏有一個方法 -
a = np.arange(1,1001) # Input array to extract numbers from
# Select 100 random unique numbers from input array and get also store leftovers
p1 = np.random.choice(a,size=100,replace=0)
p2 = np.setdiff1d(a,p1)
# Get random indices for indexing into p1 and p2
p1_idx = np.random.randint(0,p1.size,(8000))
p2_idx = np.random.randint(0,p2.size,(2000))
# Index and concatenate and randomize their positions
out = np.random.permutation(np.hstack((p1[p1_idx], p2[p2_idx])))
讓運行後的驗證 -
In [78]: np.in1d(out, p1).sum()
Out[78]: 8000
In [79]: np.in1d(out, p2).sum()
Out[79]: 2000
2
這可以很容易地完成使用1致電random.randint()
選擇一個列表和另一個電話random.choice()
在正確的列表上。我假設列表frequent
包含100個要選擇的元素80
百分比和rare
包含900
個元素要選擇的百分比20
百分比。
import random
a = random.randint(1,5)
if a == 1:
# Case for rare numbers
choice = random.choice(rare)
else:
# case for frequent numbers
choice = random.choice(frequent)
相關問題
- 1. 隨機選擇Python列表中的列表,取決於權重
- 2. 如何在Python中添加隨機權重列表?
- 3. 如何建立加權隨機列表?
- 4. Python列表中的隨機數(x.difference)
- 5. Python隨機列表
- 6. C中的隨機加權#
- 7. 在python中創建隨機數列表
- 8. coffeescript加權隨機數
- 9. PHP加權隨機數
- 10. 列表中的python隨機序列
- 11. R中的加權隨機數生成
- 12. 隨機加權陣列的權重小於1和小數
- 13. Python列表隨機選擇
- 14. 列表中的隨機元素 - Python
- 15. Python中的隨機列表選項
- 16. C++倒加權隨機播放/隨機
- 17. 隨機做動作與加權值
- 18. 從排序列表中加權隨機選擇
- 19. 從加權概率列表中隨機選擇
- 20. 從加權列表中選擇一個隨機項目
- 21. 加權隨機數生成的Javascript
- 22. Python |如何添加元素添加到列表中隨機
- 23. 帶兩個權重的加權隨機
- 24. 隨機在python列表中挑選
- 25. Python不從列表中隨機選擇
- 26. Python |避免從列表中隨機選擇以前的值
- 27. 從Python中的列表中隨機採樣函數的語法
- 28. T-SQL中的隨機加權選擇
- 29. 隨機加權子圖
- 30. 加權隨機選號
你能更具體?這100個號碼是否連續放置? – martianwars
不,他們可以分散,但應該在1和1000之間。 –
爲什麼你需要一個特殊功能呢?只需使用'random.randint()'2次。第一次選擇100與其餘的和下次選擇其中 – martianwars