---
# System prepended metadata

title: 1. Secretary problem by simulation

---

# 1. Secretary problem by simulation

## Problem Description:

We will see N=100 job candidates whose levels are random between 0 and 10. We see the candidates one-by-one, and after seeing each candidate, we have to make a decision on the spot whether to hire this candidate or not. This decision is irrevocable and we cannot change our mind later! We want to _maximize the probability to hire the best out of the N candidates_.

To do so, we will implement the following policy:

* Reject the first R candidates.
* Then hire the first one we see that is better than the best among the first R.

Write a program that decides the optimal threshold R using simulations.


```python=
def secretary_simulation(N=100, R):
    candidate_levels = []
    for candidate in range(R):
        level = random(0, 10)
        candidate_levels.append(level)
    max_level = max(candidate_levels)
    for candidate in range(R,N):
        level = random(0,10)
        if level > max_level:
            return level
    return -999
            
def run_simulation(N=100):
    best_level = 0
    for R in range(0, N):
        chosen_candidate_level = secretary_simulation(N, R)
        if chosen_candidate_level > best_level:
            best_level = chosen_candidate_level
            best_R = R
    return best_R
        

all_R = []
for i in range(1000):
    best_R = run_simulation()
    all_R.append(best_R)
    
def max_dict(d):
    max_value = 0
    for key, value in d.items():
        if value > max_value:
            max_value = value
            max_key = key
    return key


def mode(arr):
    dict_count = {}
    for a in arr:
        if a in dict_count:
            dict_count[a] += 1
        else:
            dict_count[a] = 1
    return max_dict(dict_count)
        


best_R = mode(all_R)
    
    

        


```
