---
# 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=
trials = t
ncandidates = 100

outcomes = [[False]*t for i in range(ncandidates)]

for i in range(t):
    cands = [rand()*10 for i in range(ncandidates)]
    for R in range(candidates):
        seen = cands[:R]
        bestseen = max(seen)
        newbest = 0
        for Rp in range(R:ncandidates):
            if cands[Rp] > maxseen:
                newbest = cands[Rp]
                break
        if newbest==max(cands):
            outcomes[R][i] = True

bestfound = [sum(Rset) for Rset in outcomes]
highest = [True if rpeak==max(bestfound) for rpeak in bestfound]
for i in len(bestfound):
    if highest[i]:
        print(i)
    

```
