Reputation: 139
say i generated a set of random numbers and put them into an array, (pre-sorted for simplicity) i'll use javascript to show the math:
var vals = new Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,...,10000);
then in a loop, i created a random number to serve as an index:
for(i=0;i<10000;i++){
var random_index = Math.floor(Math.random() * 10000);
var result = vals[random_index];
}
if you looked at this output on any kind of graph, the results (with enough iterations through that loop, will look pretty random and balanced)
what I want, is for the results of each access to favor smaller numbers, which incidentally here is array elements with a lower index.
for a visual example, imagine you're trying to plot on a map where the fragments of a firework fell. most of those fragments would fall randomly within a vicinity but have a heavier concentration towards the middle. that's a bit of an over-complicated example since it's taking another dimension into account and uses physics to achieve the result, but it's the same principal.
what operation should i perform on the random_index variable to make it 'favor' smaller numbers?
Upvotes: 3
Views: 2490
Reputation: 38520
There are any number of things you can do.
For instance:
Math.floor(Math.sqrt(Math.random() * 10000^2));
The real question is, what kind of distribution do you want?
Upvotes: 0
Reputation: 234807
The usual approach (as explained here, for instance) is to define the distribution function you want and then use one of two approaches to transform a uniformly distributed random variable into a random variable with the desired distribution. Provided your target distribution is simple enough, you can use inverse transform sampling.
If you don't really care what the distribution function is like, just that it favors smaller values, a simple approach might be to generate a uniform r.v. in the range [min2, max2] and then take its square root as your r.v.
Upvotes: 2