Home Backend Development Python Tutorial python概率计算器实例分析

python概率计算器实例分析

Jun 10, 2016 pm 03:16 PM
python Probability calculator

本文实例讲述了python概率计算器实现方法。分享给大家供大家参考。具体实现方法如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

from random import randrange

#randrange form random module

def calc_prob(strengths):

  """A function that receives an array of two numbers

  indicating the strength of each party

  and returns the winner"""

  if strengths[1]>strengths[0]:

#Bring the bigger number to the first position in the array

    temp=strengths[0]

    strengths[0]=strengths[1]

    strengths[1]=temp  

  prob1=abs(strengths[0]-strengths[1])

#The relative strength of the 2 parties

  prob2=randrange(0,100)

#To calculate the luck that decides the outcome

  if prob2 in range(0,33-prob1):

#Check if the weaker party is capable of winning.

#The condition gets narrower with the increase

#in relative strengths of each parties

    return strengths[1]

  elif prob2 in range(33-prob1,66-prob1):

  #The middle condition

    return "Draw"

  else:

     return strengths[0]

#Luck favors the stronger party and if relative strength

#between the teams is too large,

#the match ends up in favor of the stronger party

#Example

calc_prob([50,75]);#Always has to be a list to allow exchange

#Can be programmed in hundreds of better ways. Good luck!

Copy after login

希望本文所述对大家的Python程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Quantile regression for time series probabilistic forecasting Quantile regression for time series probabilistic forecasting May 07, 2024 pm 05:04 PM

Quantile regression for time series probabilistic forecasting

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step May 06, 2024 pm 03:52 PM

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

A complete guide to golang function debugging and analysis A complete guide to golang function debugging and analysis May 06, 2024 pm 02:00 PM

A complete guide to golang function debugging and analysis

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

Share several .NET open source AI and LLM related project frameworks

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

See all articles