Ok, It was hard to explain my problem in the title. I hope it made enough sense to peak the curiosity of some of you.
What I've been trying to do is find the relationship between the amount of random numbers generated (n) and the probability that an equal amount of odd and even numbers are generated. (n/2 odd and n/2 even). It should be clear that n must be an even number.
Obviously the numbers needn't be odd and even, as long as there is a 50% chance of generating one or the other. If this is posted in wrong location can someone navigate me in the right direction.
Anyway, basically I calculated mathematically that for every n amount of numbers generated, it should take on average approximately (4/5)sqrt(n) trials before the outcome is a perfect 50/50 (odd and even).
I got this result in terms of sqrt(n) using Stirling's approximation of factorials because I thought the final result would be much more comprehensible and easier to compare to my computed results.
However, my computed averages give me the result of approximately (6/5)sqrt(n) and I can't figure out why. I used two different pseudo-random generators (Math.random() & java.util.Random()) and both yielded the same results.
Below is the Maths involved in calculating the probability and I will also provide the code.
Please zoom in on the image if you can't read it.
Computer program for calculating computationally:
public static void main(String[] args) {
int run = 0;
int total = 0;
int amount = 0;
int count = 0;
int randint;
int odd;
int even;
int n =100; // Amount of numbers generated randomly
int precision = 10000; // Amount of averages taken into account
// (Reduce with increase in "n" as time increases)
Random generate = new Random(100); // Seed 100 at beginning of program
run = 1;
while(run==1){
odd = 0;
even = 0;
for(int i=0; i<n; i++){
randint = generate.nextInt(2);
if(randint%2==0){
even++;
}else{
odd++;
}
}
count++; // increment count after every check
if((odd==(n/2)) && (even==(n/2))){
total+=count; // add count to total ( addition before addition - average calculation)
amount++; // increment amount, this will be the division later
count=0;
}
if(amount==precision){
System.out.println(total/amount); //print average
run=0;
}
}
}