I should have provided more of a description either here or in the code. The code starts out with green circles which represent food and orangish circle that represent bugs that eat the food. When the bugs get enough food they split and randomly evolve in speed and/or direction (vector). You may have to start it a few times since it is possible for all the bugs to starve and/or die of old age. The value of food is related to the number of bugs (for balance only). In many executions the bugs will evolve to be faster. They are not programmed to get faster, this is emergent behavior based on the model. I have another version which traces thier movement (but it make them look spermlike so I didn't post it). The code uses tables, map wrap (something that goes off to the left reappears on the right), vectors (for distance/collision/eating). A few bugs stop sometimes, I haven't spent the time to know what I may be doing wrong in relation to tables.

path[1] - this is the path the bug will take, in the orginal version they had 8 paths for north, northwest, etc. and had randomness distributed between those.

path[1].x - this is how fast the bug moves in the x direction per frame. It can be postive or negative, more than 1 or less than 1

math.random() - some number less than 1

math.random() * 2 - some number less than 2

(math.random() * 2 - 1) - some number between negative 1 and postive 1

(math.random() * 2 - 1)/5 - some number between negative 0.2 and postive 0.2

(1+(((math.random() * 2) - 1) / 5)) - some number between 0.8 and 1.2

Now it's at this point I should point out that what I'm shooting for is a random percent between 80% and 120%. There is probally a better way to get to that but I think like a business programmer so that's the sideways method I used to get to that result.

Now this gets multipled by the current x which slows it down by 20% (times 80%, or 0.8) or speeds it up by 20% (120% or 1.2) or somewhere in between.

Just getting a random number between negative 0.2 and postive 0.2 and adding it (times x) to the current x would work the same. I just didn't give it much thought so it's convoluted.

yep the question that I was asking is whether the expected value of sqrt( x2^2 + y2^2 ) is 1 given that sqrt( x1^2 + y1^2 ) is 1 and that x2 = x1 * randx where rand is uniform between 0.8 and 1.2 and y2 = y1 * randy

Don't know how to work it out analytically but from a quick excel simulation looks like it introduces a bias upwards

When you select random directions, I notice that you select each component separately. So you have two random variables, say X and Y, each uniformly distributed on (0.8,1.2). Then you form the vector V = (aX,bY), where the original vector was (a,b). The question you want to ask is then: is the length of V likely to be more or less than the length of (a,b). So you want the probability of ||(aX,bY)|| > (a,b).

Expanded out, this is asking for the probability of sqrt(a^2 X^2 + b^2 Y^2) > sqrt(a^2 + b^2). We can rearrange this to see that this occurs when a^2(X^2 - 1) > -b^2(Y^2 - 1). The probability of this depends on the values of a and b, or rather their ratio. These themselves are random variables, but their distribution is somewhat complicated as we have an initial uniform distribution on (-1,1) followed by some number of multiples of a uniform distribution on (0.8,1.2)!

Rather than work that out, we can see that however a and b are distributed, they will have the same distribution and the expected value of their quotient will (probably) be 1. I'm not sure about this bit, though. Nonetheless, seeing what happens if we assume that a = b will be illustrative so let us do that.

Then we are looking for the probability that X^2 - 1 > 1 - Y^2. As Y > 0, we can rearrange this to Y > sqrt(2 - X^2). To figure this out, we plot the line of equality, which is y = sqrt(2 - x^2). As X and Y are uniformly distributed, the probability that we want is the area of the region in the box (0.8,1.2) x (0.8,1.2) which is above this curve. Now the curve y = 2 - x bisects this box and we have sqrt(2 - x^2) =< 2 - x (less than or equal to, equality only at x = 1) so the area above the curve y = sqrt(2 - x^2) in our box is strictly greater than 1/2.

Therefore the probability that the velocity vector lengthens is more than 1/2, hence the bugs tend to get faster.

To get what you want, you should work with polar coordinates instead of cartesian ones. Pick a random angle, uniformly on (0,2pi) and a random length. However, the random length shouldn't be chosen uniformly on (0.8,1.2): the thing is that you are multiplying the length by this random factor and that, I think, changes the probability distribution. I'm not a probabilist so I don't know exactly how things change here and I don't want to say anything definite because the probability (!) is that I'll get it wrong.

## Comments

I should have provided more of a description either here or in the code. The code starts out with green circles which represent food and orangish circle that represent bugs that eat the food. When the bugs get enough food they split and randomly evolve in speed and/or direction (vector). You may have to start it a few times since it is possible for all the bugs to starve and/or die of old age. The value of food is related to the number of bugs (for balance only). In many executions the bugs will evolve to be faster. They are not programmed to get faster, this is emergent behavior based on the model. I have another version which traces thier movement (but it make them look spermlike so I didn't post it). The code uses tables, map wrap (something that goes off to the left reappears on the right), vectors (for distance/collision/eating). A few bugs stop sometimes, I haven't spent the time to know what I may be doing wrong in relation to tables.

That's cool. Interesting how they all end up going approx in the same direction too even though the direction is not necessarily selected for.

I do think the code introduces a bias towards faster bugs. Or at least it's not clear to me that the expected value of the new speed is == 1

x=bugs[i].path[1].x * (1+(((math.random() * 2) - 1) / 5))

path[1] - this is the path the bug will take, in the orginal version they had 8 paths for north, northwest, etc. and had randomness distributed between those.

path[1].x - this is how fast the bug moves in the x direction per frame. It can be postive or negative, more than 1 or less than 1

math.random() - some number less than 1

math.random() * 2 - some number less than 2

(math.random() * 2 - 1) - some number between negative 1 and postive 1

(math.random() * 2 - 1)/5 - some number between negative 0.2 and postive 0.2

(1+(((math.random() * 2) - 1) / 5)) - some number between 0.8 and 1.2

Now it's at this point I should point out that what I'm shooting for is a random percent between 80% and 120%. There is probally a better way to get to that but I think like a business programmer so that's the sideways method I used to get to that result.

Now this gets multipled by the current x which slows it down by 20% (times 80%, or 0.8) or speeds it up by 20% (120% or 1.2) or somewhere in between.

Just getting a random number between negative 0.2 and postive 0.2 and adding it (times x) to the current x would work the same. I just didn't give it much thought so it's convoluted.

yep the question that I was asking is whether the expected value of sqrt( x2^2 + y2^2 ) is 1 given that sqrt( x1^2 + y1^2 ) is 1 and that x2 = x1 * randx where rand is uniform between 0.8 and 1.2 and y2 = y1 * randy

Don't know how to work it out analytically but from a quick excel simulation looks like it introduces a bias upwards

I think you may have discovered the key to our real estate over valuations :-D

When you select random directions, I notice that you select each component separately. So you have two random variables, say

`X`

and`Y`

, each uniformly distributed on`(0.8,1.2)`

. Then you form the vector`V = (aX,bY)`

, where the original vector was`(a,b)`

. The question you want to ask is then: is the length of`V`

likely to be more or less than the length of`(a,b)`

. So you want the probability of`||(aX,bY)|| > (a,b)`

.Expanded out, this is asking for the probability of

`sqrt(a^2 X^2 + b^2 Y^2) > sqrt(a^2 + b^2)`

. We can rearrange this to see that this occurs when`a^2(X^2 - 1) > -b^2(Y^2 - 1)`

. The probability of this depends on the values of`a`

and`b`

, or rather their ratio. These themselves are random variables, but their distribution is somewhat complicated as we have an initial uniform distribution on`(-1,1)`

followed by some number of multiples of a uniform distribution on`(0.8,1.2)`

!Rather than work that out, we can see that however

`a`

and`b`

are distributed, they will have the same distribution and the expected value of their quotient will (probably) be`1`

. I'm not sure about this bit, though. Nonetheless, seeing what happens if we assume that`a = b`

will be illustrative so let us do that.Then we are looking for the probability that

`X^2 - 1 > 1 - Y^2`

. As`Y > 0`

, we can rearrange this to`Y > sqrt(2 - X^2)`

. To figure this out, we plot the line of equality, which is`y = sqrt(2 - x^2)`

. As`X`

and`Y`

are uniformly distributed, the probability that we want is the area of the region in the box`(0.8,1.2) x (0.8,1.2)`

which isabovethis curve. Now the curve`y = 2 - x`

bisects this box and we have`sqrt(2 - x^2) =< 2 - x`

(less than or equal to, equality only at`x = 1`

) so the area above the curve`y = sqrt(2 - x^2)`

in our box is strictly greater than`1/2`

.Therefore the probability that the velocity vector

lengthensis more than`1/2`

, hence the bugs tend to get faster.To get what you want, you should work with polar coordinates instead of cartesian ones. Pick a random angle, uniformly on

`(0,2pi)`

and a random length. However, the random length shouldn't be chosen uniformly on`(0.8,1.2)`

: the thing is that you aremultiplyingthe length by this random factor and that, I think, changes the probability distribution. I'm not a probabilist so I don't know exactly how things change here and I don't want to say anything definite because the probability (!) is that I'll get it wrong.Thx, that's something to consider

I found an article explaining the probability distribution of a product of uniformly distributed random variables. The simple answer is: it's yukky.

Added to wiki https://bitbucket.org/TwoLivesLeft/codea/wiki/UserContrib

I think I've gotten better at using random since then