简体   繁体   中英

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 ). 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.

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

Then you choose a random value from set of <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.

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--). 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.

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