Skip to content
Advertisement

Trying to run a test to see how many children it takes to have one of each sex but the array is not working properly?

I am running some code for a class and I have no clue what to do, I submitted the assignment but it’s not helpful if I don’t know what to do in the end. Here is the code that I am running to try and track these tests. The idea is to see how many children it takes before you have at least 1 child of each sex. Then the program should be storing that to a list value as boysandgirls_stats[x]. The problem is that it won’t store all tests. It only logs around 40 out of 100 or 200 out of 1000. I don’t know where these other tests are being stored. stdarray is something we have to use from a book we are using. It tracks the tests similarly to how a list would in python but it wasn’t explained well to me. Thank you!

import stdio
import random
import sys
import stdarray

numTrials = int(sys.argv[1])

boysandgirls_stats = stdarray.create1D(4, 0)
boys = 0
girls = 0
children = 0
totalChildren = 0


for x in range(numTrials):  # Simulation to determine sex
    childType = random.randint(0, 1)
    if childType == 0:
        boys += 1
        children += 1
        totalChildren += 1
    else:
        girls += 1
        children += 1
        totalChildren += 1
    for x in range(numTrials):  # Keeps track of amount of children needed
        if (boys >= 1) and (girls >= 1):
            if children <= 5:
                boysandgirls_stats[children - 2] += 1
                boys = 0
                girls = 0
                children = 0
            else:
                boysandgirls_stats[3] += 1
                boys = 0
                girls = 0
                children = 0
avgChildren = totalChildren / numTrials  # Calculation for average children
stdio.writeln('Avg # of children: ' + str(avgChildren))
for x in range(len(boysandgirls_stats)):
    stdio.writeln(str(x+2) + ' children ' + str(boysandgirls_stats[x]))

Advertisement

Answer

import sys
import random

## let's put the test inside a function:
def child_test():
  girl = False
  boy  = False
  total_children = 0
  while not (boy and girl): 
    total_children += 1
    if random.randint(0, 1) == 0:
      boy = True
    else:
      girl = True
  return total_children


## now the statistics:
# get the number of trials from user command:
num_trials = int(sys.argv[1])

# for every number of children, log the associated number of tests:
total = 0
indexed_count = {}
for i in range(num_trials):
  num_children = child_test()
  if num_children not in indexed_count:
    indexed_count[num_children] = 0
  indexed_count[num_children] += 1
  total += num_children

# calculate and output the average:
average = total / num_trials
print(f'average is : {average}')

# show tries per number of children
sorted_indexed_count = sorted(indexed_count.items(), key = lambda x: x[1], reverse = True)
for t in sorted_indexed_count:
  print(f'  {t[0]} children : {t[1]} tests')

Outputs :

$ python3 so.py 100000
average is : 3.00812
  2 children : 49702 tests
  3 children : 25238 tests
  4 children : 12401 tests
  5 children : 6314 tests
  6 children : 3159 tests
  7 children : 1528 tests
  8 children : 840 tests
  9 children : 410 tests
  10 children : 214 tests
  11 children : 99 tests
  12 children : 53 tests
  13 children : 18 tests
  14 children : 10 tests
  15 children : 6 tests
  16 children : 6 tests
  18 children : 1 tests
  17 children : 1 tests
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement