dimanche 23 mai 2021

Strange randomness behaviour

Trying to make ants simulator using javaFX

I've had a problem that : the ants are not distributed equally on the whole frame. At first I had a problem to periodically update each ant at a random rate and to a random direction so say for example an ant will change it's direction after 0.5 s and after that it will change it randomly after 0.3s ..etc I've managed to solve this using this method,it's a bit complicated I'll try to make it as simple as I can:

Create an array chosenAnts which has a length of antCount/2 , this array , holds the indices of the randomly chosen ants that will change their direction in the next X seconds.

Using PauseTransition I loop through the chosenAnts array and then update the positions of the chosen ants , these ant will change their directions at the same moment to a random directions .

Update the chosenAnts array randomly , this provides the next random indices that'll be chosen for the next queue:

for(int i = 0 ; i < chosenAnts.length ;i++){
chosenAnts[i] = (int)(Math.random() * antCount);
}

Finally,update the position of the ants by updating the velocity on the x,y just like this with the above code, note that xvel and yvel are arrays that hold the velocity of each ant on both x ,y ; and please dont look to the right side it's just a mess in order to make the ant behave correctly and not crazily, in other words, the direction will be within the red triangle :

for(int i = 0 ; i < chosenAnts.length ;i++){
    chosenAnts[i] = (int)(Math.random() * antCount); 
            xvel[chosen[i]] = ((xvel[chosen[i]] - 0.2) + Math.random() * (xvel[chosen[i]] + .2)) % 1;
            yvel[chosen[i]] = ((yvel[chosen[i]] - 0.2) + Math.random() * (yvel[chosen[i]] + .2)) % 1;
    }

And this is how it looks like with 10 000 ants ,after few minutes of the start of the sim , note that the position of the colony is literally on the buttom right which is the opposite of where they are dense and there are borders.

enter image description here

enter image description here

enter image description here




Aucun commentaire:

Enregistrer un commentaire