# Reproduction behavior

**URL:** <https://discourse.processing.org/t/reproduction-behavior/1740>\
**Category:** Processing\
**Created:** [July 13, 2018, 11:07am UTC](https://discourse.processing.org/t/reproduction-behavior/1740 "2018-07-13T11:07:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [July 13, 2018, 11:07am UTC](https://discourse.processing.org/t/reproduction-behavior/1740/1 "2018-07-13T11:07:45Z")

</div>

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();
				DNA2 dadgenes = h3m.getDNA2();
				DNA2 childDna2 = momgenes.crossover(dadgenes, parent);
				if(parent.random(1) < 0.009){
					if(parent.random(10) > 5){
						herb3f.add(new Herbivor3F(h3fpos, childDna2, parent));
					}
					if(parent.random(10) < 5){
						herb3m.add(new Herbivor3M(h3fpos, childDna2, parent));
					}
				}
			}
		}
	}
```

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 13, 2018, 12:42pm UTC](https://discourse.processing.org/t/reproduction-behavior/1740/2 "2018-07-13T12:42:46Z")

</div>

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

```auto
if(parent.random(10) > 5){
  herb3f.add(new Herbivor3F(h3fpos, childDna2, parent));
}
if(parent.random(10) < 5){
  herb3m.add(new Herbivor3M(h3fpos, childDna2, parent));
}

```

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

```auto
float rand_value = parent.random(10);
if(rand_value > 5){
  herb3f.add(new Herbivor3F(h3fpos, childDna2, parent));
}
if(rand_value< 5){
  herb3m.add(new Herbivor3M(h3fpos, childDna2, parent));
}

```

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [July 13, 2018, 1:08pm UTC](https://discourse.processing.org/t/reproduction-behavior/1740/3 "2018-07-13T13:08:41Z")

</div>

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 ?

tanks for your time.

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 16, 2018, 7:53am UTC](https://discourse.processing.org/t/reproduction-behavior/1740/4 "2018-07-16T07:53:22Z")

</div>

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…

> [@MxFxM](#):
>
> h3m.lastChildTime() \> 10000, && h3f.lastChildTime() \> 10000

…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:

> [@MxFxM](#):
>
> void setLastChildTime(int time) { lastChildTime = time; }

The last function…

> [@MxFxM](#):
>
> int lastChildTime() { return millis() - lastChildTime; }

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 🙂

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [July 16, 2018, 10:16am UTC](https://discourse.processing.org/t/reproduction-behavior/1740/5 "2018-07-16T10:16:26Z")

</div>

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