Download

Incorrect calculations, Can't found the cause.

I’ve start studing BP yesterday. I already did some tutorials but none of them can help me with it. I’m trying to subdivide a line (a trunk in this case) to few pieces with a random size. This code have two inputs: Subdivision ( which is an integer - number of subdivisions) and Length ( length of the line). I implemented this code in JS to check how it’s works and have an actual code at front of me, but when I did it in BP I’ve get a wrong results. Could you explain to me where I’ve get a mistake? Thanks everybody for any help.

HERE IS MY BP:

HERE IS THE CODE WHICH I TRY TO IMPLEMENT:



var height = 100,
    subd = 5,
    divArr = ];

for(i = 0; i < subd; i++) {
  if(i < subd-1) {
    var div = height / (subd - i),
        divMax = div + div * 0.5,
        divMin = div *  0.5,
        randDiv = Math.random() * (divMax - divMin) + divMin;
    divArr* = randDiv;
    height -= randDiv;
  } else {
    divArr* = height;
  }
}

// show result of random length
console.log(divArr)

var  a = 0;
for(i = 0; i < divArr.length; i++) {
  a+=divArr*;
}
// check that numbers in array in total have the same result as it was declared in the begin
console.log(a)


HERE IS THE LINK TO JS CODE (same as above just with result in console):
http://jsbin.com/xakeliqedi/edit?js,console

Here I fix a little bug, there I forgot to decrease divider by 1 for each iteration of the loop. ( ex.: 5,4,3,2,1 )
f603ebf99ad78bb66249a04f5baa4fa6115e64ef.png

But result still incorrect:

As you can see LinSudbResult not equal Length :(, !@#$%

Fix a problem.

Try subdivisons - 2
It’s i < subd - 1, not i <= subd-1

I’m curious, did it work?