如何控制采摘数的概率是多少? [英] How to control the probability of picking a number?

查看:153
本文介绍了如何控制采摘数的概率是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个游戏,我想选择的 0 随机数 N ,我想作出这样的选择一个较高的数量将有更低的机会。

I am building a game, I want to pick a random number between 0 to n, I want to make that picking a higher number will have lower chances.

于是我问<一个href="http://stackoverflow.com/questions/30698648/how-to-pick-a-number-based-on-probability">this的问题,并根据阿米特答案,我写这样的:

So I asked this question, and based on amit answer, I wrote this:

public class Test {
    private static Random random = new Random();

    public static void main(String[] ars) {
        int n = 30;
        float x = 5f;

        int[] results = new int[n];
        for (float i = 1; i <= 10000; i++) {
            int choose = pickANumber(n, x);
            results[choose]++;
        }
        for (int i = 0; i < results.length; i++) {
            System.out.println(i + "\t" + results[i]);
        }
    }

    static int pickANumber(int n, float x) {
        ArrayList<Float> values = new ArrayList<>();
        n += 2;
        for (float i = 2; i < n; i++) {
            float value = i * (i - 1) / (2f * x);
            values.add(value);
        }
        float max = ((n - 1) * (n - 2) / (2 * x));
        return pickANumber(max, values);
    }

    static int pickANumber(float max, ArrayList<Float> values) {
        float number = random.nextFloat() * max;
        int i = Collections.binarySearch(values, number);
        if (i < 0) {
            i = i * -1 - 1;
        }
        return i;
    }
}

我有多少时间采摘每个数字10,000运行的输出。

Output of how many time I picked each number with 10,000 runs.

0   22
1   47
2   59
3   95
4   109
5   142
6   127
7   175
8   188
9   224
10  243
11  249
12  270
13  268
14  340
15  356
16  362
17  345
18  444
19  430
20  469
21  479
22  495
23  493
24  522
25  583
26  587
27  597
28  648
29  632

现在的问题是,它并没有关系 X 让我选择,我总是得到相同的输出。

The problem is that it does not matter what value of x I choose, I always get the same output.

我需要找到一个方法来解决这个算法,以便 X 会影响捡了两个不同的值之间的机会的比例,而主要思路仍将:拾较高的值将变得更加困难。

I need to find a way to fix this algorithm so x will affect the ratio of chances between picking up two different values, while the main idea will remain: Picking up higher values will be harder.

推荐答案

这将是好吗?如果您使用的添加剂的版本,你最终会具有相同的概率始终。我使用了更新的乘法的版本。

Would this be ok? If you use the additive version, you'll end up having the same probabilities always. I'm using the updated multiplicative version.

此外,使用 X'。1 获取更高价值的机会较低。而 X&GT; 1 ,否则

Also, use x<1 for lower chance of getting higher values. And x>1 otherwise.

import java.util.Arrays;
import java.util.Random;

public class Main {
    private static Random random = new Random();

    public static void main(String[] ars) {
        int n = 30;
        double x = 0.9;

        double[] aux = makeAux(n, x);
        int[] results = new int[n];
        for (float i = 1; i <= 10000; i++) {
            results[pickANumber(aux)]++;
        }
        for (int i = 0; i < results.length; i++) {
            System.out.println(i + "\t" + results[i]);
        }
    }

    static double[] makeAux(int n, double x) {
        double[] aux = new double[n];
        aux[0] = x;
        for (int i = 1; i < n; i++)
            aux[i] = aux[i - 1] + Math.pow(x, (i + 1));
        return aux;
    }

    static int pickANumber(double[] aux) {
        double number = random.nextDouble() * aux[aux.length - 1];
        int i = Arrays.binarySearch(aux, number);
        if (i < 0)
            i = -i - 1;
        return i;
    }
}

这篇关于如何控制采摘数的概率是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆