# -*- coding: utf-8 -*-
"""
Created on Wed Jun 14 12:30:32 2023

RMS 2023 - 1084 - Tirages dans des urnes
"""

import numpy as np
import numpy.linalg as alg

# matrice stochastique

def matrice_A(n):
    M = np.zeros((n+1, n+1))
    for i in range(1, n+1):
        M[i, i-1] = i/n
    for i in range(n):
        M[i, i+1] = 1-i/n
    return M 

# tirages

import numpy.random as rd

def historique(k, n):
    urne = np.ones(n, dtype=np.int16) # urne[i] est le numéro de l'urne qui contient la boule i
    X = [np.sum(urne)]
    for tirage in range(k):    # on effectue k tirages consécutifs : 
        i = rd.randint(0, n)   # on choisit une boule au hasard
        urne[i] = 1 - urne[i]  # et on la déplace
        X.append(np.sum(urne))
    return X

def tirage(k, n):
    urne = np.ones(n, dtype=np.int16)
    for tirage in range(k):    # on effectue k tirages consécutifs : 
        i = rd.randint(0, n)   # on choisit une boule au hasard
        urne[i] = 1 - urne[i]  # et on la déplace
    return np.sum(urne)

import matplotlib.pyplot as plt

plt.figure()
plt.plot(historique(1000, 10))

N = 10000

plt.figure()
echantillon = []
for i in range(N):
    echantillon.append(tirage(100, 10))
plt.hist(echantillon, bins=list(range(11)))

plt.figure()
echantillon = []
for i in range(N):
    echantillon.append(tirage(101, 10))
plt.hist(echantillon, bins=list(range(11)))
