简体   繁体   English

Java - 生成随机数字簇

[英]Java - generating random clusters of numbers

I have an array of stuff, and I want to "randomly" select stuff from that array, but I'd like the probability for it to get clusters to be higher. 我有一系列的东西,我想“随机”从该数组中选择东西,但我希望它的概率可以让集群更高。

For example: 例如:

arr = [1, 3, 4, 6, 7, 9, 10]
x = Math.random() * arr.length;
return arr[x]

If x came out to be 3, then the function would return 6. How do I increase the likelihood that the next number x will be is 2 or 4 (and then 1 and 5 and so on, with the probability curving the further away it gets from the current x ). 如果x出来是3,那么函数将返回6.我如何增加下一个数字x的可能性是2或4(然后是1和5,依此类推,概率越远越远得到当前的x )。 Is this doable? 这可行吗?

Using a normalized gaussian distribution I'd do something like this: 使用标准化的高斯分布,我会做这样的事情:

public class ClusterRandom{

    Random dice = new Random();
    int mRange;
    int mWidth = 1;
    int mMean;

    public ClusterRandom(int range, int startingMean, int...width){
        mRange = range;
        mMean = startingMean;
        if(width.length > 0) 
            mWidth = width[0];
    }

    public int nextInt(){

        int pick;        

        do{
              pick = (int) Math.round((dice.nextGaussian()*mWidth) + mMean);     
        }while(pick < 0 || pick >= mRange);

        mMean = pick;
        return pick;

    }

}

Then in your code: 然后在你的代码中:

int[] arr = new int[]{1, 3, 4, 6, 7, 9, 10};
// for example starting from index 3 which is 6
ClusterRandom clusterDice = new ClusterRandom(arr.length, 3);
// ...
// in loop
return arr[clusterDice.nextInt()];

Question is kind of generic, so will be my answer. 问题是通用的,所以我的答案也是如此。

Understanding of what you want to achieve is easiest(for me) with geometrical probability. 用几何概率来理解你想要达到的目标是最简单的(对我来说)。

Let's start with your first case, so index is random, and let's assume you have array of length 5. 让我们从你的第一种情况开始,所以索引是随机的,让我们假设你有长度为5的数组。

   0  |_|_|_|_|_| 5

Then you choose a random value from set of <0,1,2,3,4> . 然后从<0,1,2,3,4>集中选择一个随机值。 At this level of accuracy we might assume they are equally probable. 在这种准确度下,我们可能认为它们同样可能。

So what if we want to make just one of them twice as probable as the others? 那么,如果我们想让其中一个可能与其他人相比两倍呢? We widen the slot for that index. 我们扩大了该索引的插槽。

   0  |_ _|_|_|_|_| 5

Now we choose a value from set of <0,1,2,3,4,5> but, we say that both 0 and 1 mean choice of element at index 0. 现在我们从<0,1,2,3,4,5>集合中选择一个值,但我们说0和1都意味着在索引0处选择元素。

So if we kept the second array, in which we would keep width of its slot, we could still randomize the result, but it would match it against the range of values(of custom length) thus making one more/less probable than the other. 因此,如果我们保留第二个数组,我们将保持其插槽的宽度,我们仍然可以随机化结果,但它会将其与自定义长度的值范围相匹配,从而比另一个更多/更少。 。

My approach would be then to create that second array of integers, write function taking this array and the first random index, fill them with upper bound of the range (lower would be at index--). 我的方法是创建第二个整数数组,写入函数获取此数组和第一个随机索引,用范围的上限填充它们(较低的是在index--)。 That range would be related to distance from the first index, making the first random index central. 该范围与第一个索引的距离有关,使第一个随机索引成为中心。

Then you create one more function that makes randomization, matches result with array of ranges and returns corresponding element from elements array. 然后再创建一个使随机化的函数,将结果与范围数组匹配,并从元素数组中返回相应的元素。

That might be naive, but that's one I would understand. 这可能是天真的,但这是我能理解的。 Hope it helps. 希望能帮助到你。

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

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