简体   繁体   中英

Random number based on probability python

Hi I have done reseatch and I believe I ended up in the right direction when I ended up at this thread:

http://code.activestate.com/recipes/117241/

Basically my question is: what is the code in the link doing line by line. You could potentially ignore all that I wrote below if your explanation makes me understand what the code in the link does to a satisfactory extent.

I BELIEVE that the code at that link generates a random number BUT the random number is directly related to the probability.

In my own code I am attempting to take a "number" and its probability of appearing, and get an output "number", that will appear according to the probability. I know this is confusing but if you look at the link above then I hope it will be clear what I am trying to do. My code below is in reference to the link above.

so in my program, these are my global variables:

HIGH= 3
MED= 2
LOW= 1

This is the list I am working with:

n= [LOW,lowAttackProb).(MED,medAttackProb),(HIGH,highAttackProb)]
#lowAttackProb,med...,etc. are based on user input are just percents converted to decimals that add up to 1 in every case

This is how I implemented the random code as per the link above:

x= random.uniform(0,1)
for alevel,probability in n:
    if x<probability:
        break
    x=x-probability
return alevel

I am unsure exactly what is happening inside the for loop and what x=x-probability is doing.

Lets say that x=0.90

and that in my list, the chance of the second list entry occuring is 0.60, then, since x (is less than)probability is False(im not too sure what if x(is less than)probability even does), the code moves on to n=n-probability.

I really hope this makes sense. If it does not please let me know what is unclear and I will try to fix it up. Thank you for any and all help.

This code implements the selection of event taking probabilities of possible events into account. Here is the idea behind it.

There are three events (or levels as you call them), LOW , MED , HIGH , with certain nonzero probability each, and all probabilities sum up to exactly 1. Using standard means of Python one can generate a random number between 0 and 1. So how can we "map" them to each other? Lets align our probabilities (lets call them L, M, and H for brevity) along the numbers line the following way:

0__________________L______________L+M_________________________L+M+H ( = 1)

Now taking our randomly generated number x we can say that

  1. If x lies in interval [0, L] then the first event occurred.
  2. If x lies in half-interval (L, L+M] then the second event occurred.
  3. If x lies in half-interval (L+M, L+M+H] then the third event occurred.

The code you are asking about simply matches x to one of the intervals and returns the corresponding event (or level).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM