Skip to content

Instantly share code, notes, and snippets.

@AndroidDeveloperLB
Last active November 23, 2019 07:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndroidDeveloperLB/6b184f34dd2ef7e186ff to your computer and use it in GitHub Desktop.
Save AndroidDeveloperLB/6b184f34dd2ef7e186ff to your computer and use it in GitHub Desktop.
based on this video: https://youtu.be/TGLYcYCm2FM?t=106 , checks how come there are 10% left handed people, if all combinations of them are above 10%
import java.util.ArrayList;
import java.util.Random;
//based on this video: https://youtu.be/TGLYcYCm2FM?t=106
// checks how come there are 10% left handed people, if :
// left-male + right-female = 17% left handed
// right-male + right-female = 10% left handed
// right-male + left-female = 22% left handed
// left-male + left-female = 25% left handed
public class Main {
private static final int GENERATIONS = 100;
private static final int POPULATION_SIZE = 10000;
private static class Person {
boolean isLeftHanded;
public Person(final boolean isLeftHanded) {
super();
this.isLeftHanded = isLeftHanded;
}
}
public static void main(final String[] args) {
final ArrayList<Person> males = new ArrayList<>(POPULATION_SIZE / 2),
females = new ArrayList<>(POPULATION_SIZE / 2);
final Random r = new Random();
for (int i = 0; i < POPULATION_SIZE / 2; ++i) {
males.add(new Person(r.nextInt(10) == 0));
females.add(new Person(r.nextInt(10) == 0));
}
int leftTotal = getCountOfLeftHanded(males, females);
System.out.println("before, left handed count:" + leftTotal + " out of " + POPULATION_SIZE + " which is "
+ 100 * leftTotal / POPULATION_SIZE + "%");
for (int i = 0; i < GENERATIONS; ++i) {
for (int j = 0; j < POPULATION_SIZE / 2; ++j) {
final int chanceToGetLeftHandedChild;
final int chosenMaleIndex = r.nextInt(POPULATION_SIZE / 2 - j),
chosenFemaleIndex = r.nextInt(POPULATION_SIZE / 2 - j);
final Person chosenMale = males.get(chosenMaleIndex), chosenFemale = females.get(chosenFemaleIndex);
if (chosenMale.isLeftHanded)
if (chosenFemale.isLeftHanded)
chanceToGetLeftHandedChild = 25;// both left
else
chanceToGetLeftHandedChild = 17;// male-left,
// female-right
else if (chosenFemale.isLeftHanded)
chanceToGetLeftHandedChild = 22;// male right, female left
else
chanceToGetLeftHandedChild = 10; // both right
// removal of previous generation of male and female, and check
// the next couple
males.remove(chosenMaleIndex);
females.remove(chosenFemaleIndex);
// birth of male
int roll = r.nextInt(100) + 1;
if (roll <= chanceToGetLeftHandedChild)
males.add(new Person(true));
else
males.add(new Person(false));
// birth of female
roll = r.nextInt() + 1;
if (roll <= chanceToGetLeftHandedChild)
females.add(new Person(true));
else
females.add(new Person(false));
}
leftTotal = getCountOfLeftHanded(males, females);
System.out.println("generation:" + i + " left handed count:" + leftTotal + " out of " + POPULATION_SIZE
+ " which is " + 100 * leftTotal / POPULATION_SIZE + "%");
}
leftTotal = getCountOfLeftHanded(males, females);
System.out.println("after, left handed count:" + leftTotal + " out of " + POPULATION_SIZE + " which is "
+ 100 * leftTotal / POPULATION_SIZE + "%");
}
private static int getCountOfLeftHanded(final ArrayList<Person> males, final ArrayList<Person> females) {
int totalLeftHanded = 0;
for (final Person person : females)
if (person.isLeftHanded)
++totalLeftHanded;
for (final Person person : males)
if (person.isLeftHanded)
++totalLeftHanded;
return totalLeftHanded;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment