Un peu d'aléatoire II

Un peu d'aléatoire II

import random as rd
import numpy as np
import matplotlib.pyplot as plt

## Jeu du chaos

def polygone(n):
    x= [2*k*np.pi / n for k in range(n)]
    return np.cos(x),np.sin(x)

# x,y = polygone(10000)
# plt.plot(x,y,linestyle='',marker = '.')
# plt.axis('equal')
# plt.show()

def chaos(n,N):
    px,py = polygone(n)
    abs = [0]
    ord = [0]
    for _ in range(N):
        sommet = rd.randint(0,n-1)
        x = (abs[-1] + px[sommet])/2
        y = (ord[-1] + py[sommet])/2
        abs.append(x); ord.append(y)
    plt.clf()
    plt.axis('equal')
    plt.plot(abs,ord,marker=',',linestyle='')
    plt.draw(); plt.show()

def chaos2(n,N):
    px,py = polygone(n)
    abs = [0]
    ord = [0]
    mem=-1
    sommet = rd.randint(0,n-1)
    for _ in range(N):
        while sommet == mem:
            sommet = rd.randint(0,n-1)
        mem = sommet 
        x = (abs[-1] + px[sommet])/2
        y = (ord[-1] + py[sommet])/2
        abs.append(x); ord.append(y)
    plt.clf()
    plt.axis('equal')
    plt.plot(abs,ord,marker=',',linestyle='')
    plt.draw(); plt.show()

## PageRank

mat = [
[0,1,1,0,0,0,0,0,0,0,0,1],
[1,0,1,0,0,0,0,0,0,0,0,1],
[1,1,0,0,0,0,0,0,0,0,0,1],
[0,1,1,0,0,1,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0,0,0,0,0],
[0,0,0,1,0,0,1,1,0,0,0,0],
[0,0,0,0,0,1,0,1,0,0,0,0],
[0,0,0,0,0,1,1,0,0,0,0,0],
[0,0,1,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,0,1,0,1,0],
[0,0,0,0,0,0,0,0,0,0,1,0],
[0,0,0,0,0,0,0,0,0,0,0,1]
]

def voisins(i):
    l = []
    for j in range(len(mat)):
        if mat[i][j] == 1:
            l.append(j)
    return l

def pagerank(N):
    n = len(mat)
    rank = [0 for _ in range(n)]
    site = rd.randint(0,n-1)
    for _ in range(N):
        if rd.random() < .15:
            site = rd.randint(0,n-1)
            rank[site] += 1/N
        else:
            v = voisins(site)
            site = v[rd.randint(0,len(v)-1)]
            # site = rd.choice(v)
            rank[site] += 1/N
    return rank

rank = [round(elem,3) for elem in pagerank(1000000)]
print(rank)