> I would like to know how to coose elements from an
> array(or similar data structure)
So you access an element by an integer index?
>according to a
> probability distribution. For example a higher
> probability of choosing the elements nearer the
> start.
>
> Any tips or hints would be greatly appreciated
You start from a uniform random number generator, which presumably generates a double in [0, 1[ uniformly. From this you can derive a random int in [0, N[ according to any distribution, by dividing [0, 1[ into N intervals. You look in which interval each new random number falls; if it is in the n-th interval your random index is n.
Now, if you make the N intervals equal, each index has a 100/N % probability, and the result is again uniformly distributed.
If you make the N intervals unequal, you get a different result distribution. The length of each interval corresponds to the chance that the next uniform random double falls within it.
For example, with N = 4, intervals [0, 0.25[, [0.25, 0.5[ , ... yields a uniformly random int in [0, 4[. Intervals [0, 0.4[, [0.4, 0.7[ , [0.7, 0.9[ , [0.9, 1[ (numbers chosen pseudo-arbitrarily) yields something like what you wanted for example: 40% for 0, 30% for 1, 20% for 2, 10% for 3.
So you'll need to first calculate, given N and the desired result distribution, what you want the lengths of each interval to be.
> So you'll need to first calculate, given N and the
> desired result distribution, what you want the
> lengths of each interval to be.
Have you any idea how I would go about calculating this result distribution? I've had a look into probability distributions but I'm struggling. I would like to have the first interval to be the largest and the intervals to get smaller, not necessarily in a linear fashion. I can't hard code this because the size of my set is dynamic.
Thanks in advance
> > So you'll need to first calculate, given N and the
> > desired result distribution, what you want the
> > lengths of each interval to be.
>
> Have you any idea how I would go about calculating
> this result distribution?
You don't calculate the desired distribution; I assume you know what you want it to be. You want to calculate the interval lengths.
> I've had a look into
> probability distributions but I'm struggling. I
> would like to have the first interval to be the
> largest and the intervals to get smaller, not
> necessarily in a linear fashion. I can't hard code
> this because the size of my set is dynamic.
The length of each interval corresponds to the chance that the next uniform random double falls within it.
So, you want a given distribution, let's say "P(x) ~= 1/(1 + x)"; this satisfies your requirement. You can use any P(x) you want though.
P(0) = 1; P(1) = 1/2; P(2) = 1/3; ... ; P(N - 1) = 1/N
So these are non-normalized probabilities (don't add up to 1). Calculate the sum. Divide each probability by that sum. This gives you a normalized set of probabilities: they satisfy the initial distribution, and they all add up to 1. So you can use these as the lengths of your intervals.