Python uses weighted random numbers to solve lottery drawing and game Brute Force equipment problems. python random numbers

Source: Internet
Author: User

Python uses weighted random numbers to solve lottery drawing and game Brute Force equipment problems. python random numbers

About weighted random numbers
For better understanding, Let's first look at the comparison of three types of random problems:
1. n records exist, and m records are selected from them. The order of the selected records is different from that of the selected records.
Implementation idea: traverse all records by row and retrieve one data every n/m records.
2. In the first case, the m records selected must be sorted randomly.
Implementation idea: Add a column of tags for n records, and the values are randomly selected from 1 to n without repeated data.
3. Different from the 1 and 2 types of problems, if the record has a weight, how can we randomly select the records based on the weight. For example, if the weight of A is 10, the weight of B is 5, and the weight of C is 1, then AABB may appear when four weights are randomly selected.
This article focuses on the 3rd category of issues.
Implementation Method: Take four random records A: 10, B: 5, C: 1 as an example. (It doesn't matter whether weights are sorted)
For
A 10
B 5
C 1
First, assign the value of row n to row n plus row n-1 and perform recursion as follows:
A 10
B 15
C 16
Then, A random number is selected from [] each time. If it falls between [], A is selected. If it falls between (], B is selected, if the value falls between (16, 16], select C, as shown in the figure below. The selected range is large (with a high weight), and the probability of being selected is greater.

Application in lottery drawing and game explosion Equipment
Random use in Game Development, various Lottery and explosive equipment.
The operation configures the probability that each item appears as needed.
The idea of this weighted random algorithm today is very simple, that is, "All items are divided into Intervals Based on their weights, with a large range of weights. it can be imagined as a pie chart. then, throw the dice to see which interval it falls ,"
For example, there is a year-end Lucky Draw for iphone/ipad/itouch.
The weights configured by the organizer are [('iphone ', 10), ('ipad', 40), ('itouch', 50)].
Use a line of code to illustrate the concept, that is, random. choice (['iphone '] * 10 + ['ipad'] * 40 + ['itouch'] * 50 ).
Next, we will write a general function.

# Coding = UTF-8 import random def weighted_random (items): total = sum (w for _, w in items) n = random. uniform (0, total) # Throw the dice for x and w in items in the pie chart: # traverse the interval where the dice are located if n <w: break n-= w return x print weighted_random ([('iphone ', 10), ('ipad', 40), ('itouch', 50)])

The above code is intuitive enough, but we will find that total is calculated every time, and the range is linearly traversed each time for the subtraction operation. in fact, we can save it first, just look up the table. use accumulate + bisect for binary search.
The more items, the more performance the binary search improves.

# Coding = UTF-8 class WeightRandom: def _ init _ (self, items): weights = [w for _, w in items] self. goods = [x for x, _ in items] self. total = sum (weights) self. acc = list (self. accumulate (weights) def accumulate (self, weights): # tired and. for example, accumulate ([100, 50])-> [,] cur = 0 for w in weights: cur = cur + w yield cur def _ call _ (self): return self. goods [bisect. bisect_right (self. acc, random. uniform (0, self. total)] wr = WeightRandom ([('iphone ', 10), ('ipad', 40), ('itouch', 50)]) print wr ()

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.