Get N values between 0 and 1 AND control spacing of results.

223 Views Asked by At

So I need to get N number of values between 0 and 1. The values should be evenly spaced. Thats easy...

However, I also want another variable X that will shift the resulting values closer to 0 or 1.

So for example,

N = 4
X = 0 
result = .2, .4, .6, .8  (evenly spaced)

Changing X would result in something like,

N = 4
X = .5
result = .35, .6, .75, .85   (more numbers closer to 1)

As well as

N = 4
X = -.5
result = .15, .25, .4, .7   (more numbers closer to 0)

I feel like the answer should be simple....

Currently, this seems to work for shifting values closer to 0, where i is the current result being calculated. ie, if N = 4 and X = .5 the results are, .11, .25, .43, .67

$$ r_i = \frac {i-(X*i)} {N + 1 - (X*i)}$$

2

There are 2 best solutions below

0
On

You could try something like $$ a_i = \frac i{N+1} + X\cdot \frac{i(N+1-i)}{(N+1)^2}$$

0
On

If I understand correctly, you want to split the interval $[0,1]$ into $N$ subintervals, the length of which will decrease or increase with a factor of $x$. This means the first interval will have length $a$ for some $a$, the second will have length $ax$, the third $ax^2$ and so on.

Since you will split the interval into $n$ intervals and the length of the $i$-th interval is $a^{i-1}$, the total length of the itnervals is $$a+ax+ax^2+\dots + ax^{n-1}=a\frac{x^{n}-1}{x-1}.$$

You want this total length to equal $1$, meaning that the $a$ you are looking for is $$a=\frac{x-1}{x^{n}-1}.$$

This means your first point is $x_0=a$, your second point is $x_1=a+ax$, the third is $x_2=a+ax+ax^2$ and so on. You can calculate these points easily, as $x_{n+1} = x_n + ax^{n+1}$.