Python implementation of 2 methods based on weighted random number

Source: Internet
Author: User

This article mainly introduces Python to implement the weight of random number 2 methods, this article directly gives the implementation code, the need for friends can refer to the


For example, we have to choose from different provinces to choose a number, each province's weight is not the same, direct selection of random number is certainly not the case, you need a model to solve this problem.

Simplify to the following questions:

The key of the dictionary is the province, and value represents the weight, we now need a function, each time based on the weight to choose a province out

{"A": 2, "B": 2, "C": 4, "D": Ten, "E": 20}


This is the most able to think and see the version, do not know that there is no more efficient and easy to use the algorithm.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The #!/usr/bin/env python #-*-coding:utf-8-*-#python2 7x #random_weight. py #author: 2014-10-11 &NB Sp "' every element has a weight, then according to the weight random value   Input {" A ": 2," B ": 2," C ": 4," D ": Ten," E ": 20} Output a value '" ' Import random import collections as C Oll   data = {"A": 2, "B": 2, "C": 4, "D": 6, "E": one}   #第一种 according to element weight value "A" *2. And so on, put each element weight element into an array, and then the most array subscript random number to get weight def list_method (): All_data = [] for V, W in Data.items (): temp = [] for I in range (w ): Temp.append (v) all_data.extend (temp)   n = random.randint (0,len (All_data)-1) return All_data[n]   #第二种 also to be calculated The sum of weights, take out a random number, iterate through all the elements, add the weights sum, when sum is greater than or equal to the random number stop, remove the current tuple def iter_method (): total = SUM (Data.values ()) rad = Random.randint (1,total)   Cur_total = 0 res = "" For K, v. in Data.items (): Cur_total + = v if rad<= cur_total:res = k break return res     DEF Test (method): Dict_num = coll.defaultdict (int) for I in Range (m): Dict_num[eval (method)] + + 1 for i,j in Dict_num.items (): Print I, J   if __name__ = = "__main__": Test ("List_method ()") print "-" *50 Test ("Iter_method ()")

Results of one execution


1 2 3 4 5 6 7 8 9 10 11 A 4 c b 7 E D--------------------------------------------------a 8 c B 6 E D 27
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.