---
# 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=
import random
# random.random(0,1)
def simulation(K):
    def optimal(N, R):
        array = [random.random(0,1)*10 for _ in range(100)]
        maximum = max(array[:R])
        for i in range(R, 100):
            if array[i] > maximum:
                return int(array[i] == max(array))

        return 0
    
    R = [0 for _ in range(100)]
    
    for r in range(100):
        count = 0
        for i in range(K):
            count += optimal(100, r)
        
        R[r] += count
        
    return R.index(max(R))
        


```

