3

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?

kona
  • 139
  • 8
  • 1
    If you don't know it already, you'll want to read about the [normal distribution](https://en.wikipedia.org/wiki/Normal_distribution). The position of firework particles on the ground is probably normally distributed. – Tim Dec 14 '11 at 20:29
  • On second thought, I agree the normal distribution is a better fit than the exponential. – erickson Dec 14 '11 at 21:07
  • possible duplicate of [Random number generator with higher probabilities of giving low values?](http://stackoverflow.com/questions/4420502/random-number-generator-with-higher-probabilities-of-giving-low-values) – lhf Dec 14 '11 at 22:16
  • @lhf just noticed this comment. yeah, the modified version of your proposed function in that thread is generally what i was after it seems. – kona Dec 14 '11 at 22:42

2 Answers2

2

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

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?

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
  • good question, i think i'd have to see the result visually to determine what 'looks right' for the application i'm using this for. using that function, how can i manipulate the distribution? – kona Dec 14 '11 at 21:09
  • That's right. Cubic root of cube will be more skewed towards small numbers than square root of square, and so on. Similarly you can take the logarithm of exponential, which again gives you a different shape. The possibilities are endless, and they're all fine if you just want a quick, arbitrary distribution that grossly favours small numbers. But if you need something more specific, then you should consider @Ted Hopp's answer. – Jean-François Corbett Dec 15 '11 at 07:34
  • This is a wrong answer. Sqrt(R * Max^2) = Sqrt(R) * Sqrt(Max^2) = Sqrt(R) * Max, and since your random seed is less than 1, its square root will be higher, resulting in the opposite of what the OP wants. I think the approach is correct, if your random number is within the range of [1, Max^2] then its square roott will be what you're looking for, but since you're using Java functions for achieving this then you're getting it wrong. – kermit11 Nov 16 '20 at 09:50