Reproduction behavior

hi im new in programing and in processing and i use it with java in oracle, so sorry if my question is dumb or not clear.

so here is my question,
i want to make a reproduction mechanisme for my herbivor (herb3m for male and herb3f for female).
For now everytime a male touch a female, a random number is take and if the condition is make a new herbivor is added. but its not “natural” because some time a lot of “child” are added because of my condition.

what i want is:
when a male touch a female there is a chance of make a “child” (like i do in my code, for me it fine) BUT i want a delay after the first child is born and the next one so there is no demographic explosion.

so how did i make a delay AFTER the first “child” is born ?

sorry for my english and tanks for yours times.

``````	for(int x = herb3m.size()-1; x >= 0; x--){
for(int y = herb3f.size()-1; y >= 0; y--){
Herbivor3M h3m = herb3m.get(x);
Herbivor3F h3f = herb3f.get(y);
PVector h3mpos = h3m.position;
PVector h3fpos = h3f.position;
float d = PVector.dist(h3mpos, h3fpos);
if(d < h3f.getR()){
//h3f.gestation(parent);
DNA2 momgenes = h3f.getDNA2();
if(parent.random(1) < 0.009){
if(parent.random(10) > 5){
}
if(parent.random(10) < 5){
}
}
}
}
}``````

Hi,
first of all, do you know that you can get a male and a femlae at the same time using this:

``````if(parent.random(10) > 5){
}
if(parent.random(10) < 5){
}
``````

Since you calculate a random number twice. Instead you could say:

``````float rand_value = parent.random(10);
if(rand_value > 5){
}
if(rand_value< 5){
}
``````

Or use else if or you didt that in parent.random() I don’t know.

For the delay you could add a variable in the Herbivor3M/Herbivor3F class and set it to -10000 (for invalid or not used yet). Then you can go like this:

``````if(parent.random(1) < 0.009 && h3m.lastChildTime() > 10000, && h3f.lastChildTime() > 10000){
//get child
h3m.setLastChildTime(millis());
h3f.setLastChildTime(millis());
}

//variable in Herbivor3M/Herbivor3F class
int lastChildTime = -1;

//functions in Herbivor3M/Herbivor3F class
int lastChildTime() {
return millis() - lastChildTime;
}
void setLastChildTime(int time) {
lastChildTime = time;
}
``````

I think it should work.

Thanks a lot its works, i just use the third part for now, i gonna use you advices for my future code.

but i dont get it, can you explain to me how its work ?

Hi, sorry I relpy so late but I’m not active here during week ends.

The code I gave you adds another test to the if() before getting a child. In that test…

…it checks whether the last time that specific Herbivor had a child is more than 10 seconds ago. Since the time is measured in milli seconds it’s 10000.

Then when they have a child the time they got their child (in milliseconds) is saved in the variable:

The last function…

returns the difference between the saved time and the actual time. This difference is used in the test of the if() above.

I hope this helps you to understand the code. If not please ask again

Thanks a lot for your explainations. Its help me a lots.