0

I'm not really good at math, and I was trying to generate some random data but I couldn't figure how I can achieve this. I want to generate 12 bars that show 0 to 100. But I don't wanna have them all random, I'd like to have some of them in a nice curve result as below:

[1: 10], [2: 14], [3: 20], [4: 18],
[5: 22], [6: 33], [7: 62], [8: 51], 
[9: 89], [10: 27], [11: 13], [12: 56]

To clarify:

-EDIT (1)

Values will go up and down like a curve such as the picture:

The breaking point can happen anywhere.

If I'm asking too much please just show me the right direction, I'll figure it on my own.

Thanks in advance.

PRAISER
  • 793
  • 7
  • 15
  • There are multiple ways for "each bar to be depend on the previous ones." But the rest of that paragraph makes no sense. Please clarify. – Rory Daulton May 21 '17 at 17:48
  • To clarify I added an image. – PRAISER May 21 '17 at 18:24
  • Can you create a series of values with uniform slope? Can you do that a few times in a row, to make a zig-zag? Can you make the slopes and lengths random? Can you add a small random value to each bar to make it look less perfect? What's the first part you can't do? – Beta May 22 '17 at 01:48

1 Answers1

1

I would generate random curve and then just interpolate it with the bars like:

  1. generate n control points

    x(i)=i;
    y(i)=100.0*Random();
    

    you do not need the x(i) ...

  2. generate m > n bars

    so just use the n points as control points for BEZIER or any other polynomial curve and compute your bars while... Let assume simple case using single cubic n=4 then your control points will convert into polynomial:

    y(t) = a0 + a1*t +a2*t^2 + a3*t^3
    

    so compute each bar size like:

    i={0,1,2,3,...,m-1}
    t=i/(m-1); // this converts i to t=<0,1>
    bar(i) = a0 + a1*t +a2*t^2 + a3*t^3
    

    Do not forget to clamp bar(i) so it not exceeds <0,100>. This will create smooth random curve like bars.

If I put all this together I can do something like this in C++:

double  d1,d2,y0,y1,y2,y3,a0,a1,a2,a3,t;
// n=4 random control points for cubic
y0=100.0*Random();
y1=100.0*Random();
y2=100.0*Random();
y3=100.0*Random();
// convert them to interpolation cubic
d1=0.5*(y2-y0);
d2=0.5*(y3-y1);
a0=y1;
a1=d1;
a2=(3.0*(y2-y1))-(2.0*d1)-d2;
a3=d1+d2+(2.0*(-y2+y1));
// compute m bars
const int m=10;
double bar[m]; int i;
for (i=0;i<m;i++)
 {
 t=double(i)/double(m-1);
 bar[i] = a0 + a1*t +a2*t*t + a3*t*t*t;
 if (bar[i]<0.0) bar[i]=0.0;
 if (bar[i]>100.0) bar[i]=100.0;
 }

Code is untested written directly here so it may contain typos. The bars should smoothly follow the cubic curve so it should met all your needs... You can use bigger degree of curve polynomial and or piecewise interpolation in case you need more bumps. For more info see:

Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380