---
# 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

def simulate_policy(r, n, num_sims):
    count = 0

    for sim_idx in range(num_sims):
        candidate_list = [random.random() for _ in range(n)]
        
        best_among_first_r = max(candidate_list[:r])
        
        chosen_idx = -1
        for i in range(r,n):
            if candidate_list[i] > best_among_first_r:
                chosen_idx = i
                break
        
        if chosen_idx > -1:
            count += candidate_list[chosen_idx] == max(candidate_list)
    
    freq = count / num_sims
    
    return freq
        
        
def find_best_threshold(n, num_sims):
    # check input

    res = list()
    
    for r in range(0,n):
        freq = simulate_policy(r, n, num_sims)
        res.append(freq)
        
        
    # get argmax
    best_r = # jjj
        
    return best_r
        

```

