简体   繁体   English

根据Python中的变量生成随机值

[英]Generate random values based on a variable in Python

I am trying to generate a random number based on accuracy of a player. 我正在尝试根据玩家的准确性生成一个随机数。 If his accuracy is more, his cost should be more. 如果他的准确性更高,那么他的成本应该更高。

For ex, 例如,

      if accuracy >= 0.9, cost = 0.7

      else if accuracy >= 0.8, cost = 0.6
      (something like this)

cost is a random floating point number between 0 and user_specified_value. cost是介于0和user_specified_value之间的随机浮点数。 If his accuracy is closer to 1, I want cost to be generated closer to user_specified_value. 如果他的准确性接近1,我希望产生的费用更接近user_specified_value。

How do I generate the cost based on the accuracy? 如何根据准确性生成成本?

Thank you. 谢谢。

from __future__ import division ## this lets us do non integer division
import random

user_specified_value = 5

if accuracy >= 0.9:
    cost = random.randrange(user_specified_value//2,user_specified_value,1)/10
else:
    cost = random.randrange(0,user_specified_value,1)/10

Because we imported __future__ all division using / will be floating point division. 因为我们导入了__future__所有使用/除法都是浮点除法。 If you want integer division just use // . 如果要整数除法,请使用// Float division is the default in python 3. 浮点除法是python 3中的默认设置。

random.randrange doesn't like non-integers, so if you want a decimal cost you have to specify user_specified_value as an integer. random.randrange不喜欢非整数,因此,如果需要十进制成本,则必须将user_specified_value指定为整数。

What I think you're asking about is sampling from a distribution. 我想您要问的是从分发中抽样。

Python's random module provides several distributions, the choice of which depends on what you want for your application. Python的随机模块提供了几种发行版,具体选择取决于您对应用程序的需求。 For example random.random() returns floating points uniformly distributed between 0.0 and 1.0. 例如random.random()返回在0.0和1.0之间均匀分布的浮点。

Uniform 制服

You might want to sample costs uniformly from a band that gets higher with accuracy, like this: 您可能希望从准确度更高的频段中统一采样成本,如下所示:

cost = (random.random() * spread + min(accuracy, (1-spread))) * user_specified_value

Spread defines how wide the band is, and we draw uniformly within the band, sliding the band upward as accuracy increases stopping when we bump up against 100%. Spread定义了条带的宽度,我们在条带内均匀地绘制,随着精度的提高(当我们碰到100%时停止),条带向上滑动。

Let's see how this looks, generating 1000 random samples: 让我们看一下它的外观,生成1000个随机样本:

The y axis here is cost. 此处的y轴是成本。 The x axis is just the order in which the samples were drawn and is meaningless. x轴只是绘制样本的顺序,没有任何意义。 Here's what you get for low accuracy (0.01) 这就是您获得的低准确性(0.01) 精度= 0.01

Dialing the accuracy up to 0.30, get's you random costs in this range: 拨号精度高达0.30,您将获得以下范围的随机费用: 精度= 0.30

And for highly accurate players (0.99), we get: 对于高度准确的玩家(0.99),我们得到: 精度= 0.99

Triangular distribution 三角分布

Another way to scale up the costs would be to use a triangular distribution , something like this: 扩大成本的另一种方法是使用三角分布 ,如下所示:

cost = random.triangular(0.0,max(0.01,accuracy),1.0)

This time, the x axis is accuracy, the cost, y, scales up gradually. 这次,x轴是精度,成本y逐渐扩大。 Note that a highly accurate player can still get a low cost, but a low accuracy player never gets a high cost: 请注意,高准确度的播放器仍然可以获得较低的费用,但是低准确度的播放器则永远不会获得较高的费用: 三角分布

BTW, I used the plotting library matplotlib and the following code to generate these nifty plots. 顺便说一句,我使用了绘图库matplotlib和以下代码来生成这些漂亮的图。

import numpy as np
import matplotlib.pyplot as plt
import random

user_specified_value = 100.0
spread = 0.33
n = 1000

## uniform
accuracy = 0.01
costs = [0] * n
for i in range(0,n):
    costs[i] = random.random() * user_specified_value * accuracy
plt.scatter(range(0,n), costs)
plt.ylim(0, 100)

## triangular
accuracy = [0] * n
costs = [0] * n
for i in range(0,n):
    accuracy[i] = float(i)/n
    costs[i] = random.triangular(0.0,max(0.01,accuracy[i]),1.0) * user_specified_value
plt.scatter(accuracy, costs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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