---
# 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 simulation(r):
    candidataes = np.random.randint(100,(0,11)) # O(N)
    bes_cand = np.max(candidataes[:r]) # O(R)
    choosen = None
    for cand in candidataes[r:]: # O(N-R)
        if cand>bes_cand:
            choosen = cand
            break
    return choosen is not None and cand==np.max(candidataes)


def optimizer():
    m = 50
    scores = np.zeros(100)
    for r in range(1,99):
        scores[r] = np.mean([simulation(r) for _ in range(m)])
    optyimal_r = np.argmax(scores)
```