Repository navigation
Expand file tree
/
Copy pathTeorema-simulacoes.html
More file actions
68 lines (62 loc) · 2.1 KB
/
Copy pathTeorema-simulacoes.html
File metadata and controls
68 lines (62 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html>
<html lang="pt">
<head>
<meta charset="UTF-8">
<title>TED - Simulações</title>
<meta name="author" content="Lara Raquel Rodrigues Branco Arsénio">
<meta name="project-id" content="678999999">
<style>
#result {
white-space: pre-line;
font-family: monospace;
background: #f0f0f0;
padding: 10px;
border: 1px solid #ccc;
min-height: 100%;
height:100%;
overflow-y: auto;
}
</style>
</head>
<body>
<h1>Teorema da Entropia Democrática - Simulações</h1>
<p>Esta página permite simular o comportamento da mediana das preferências em democracias de larga escala.</p>
<div id="result"></div>
<script>
/*
* Teorema da Entropia Democrática - Simulações
* Autor: Lara Raquel Rodrigues Branco Arsénio
* ID do projeto: 678999999
* Licença: MIT License
*/
function gerarPreferencias(n) {
let prefs = [];
for (let i = 0; i < n; i++) {
prefs.push(Math.random());
}
return prefs;
}
function mediana(arr) {
let sorted = arr.slice().sort((a, b) => a - b);
let mid = Math.floor(sorted.length / 2);
if (sorted.length % 2 === 0) {
return (sorted[mid - 1] + sorted[mid]) / 2;
} else {
return sorted[mid];
}
}
function simularTED(n) {
let prefs = gerarPreferencias(n);
let Q = mediana(prefs) * 100;
return `Número de participantes: ${n}<br>Preferências: ${prefs.map(x => x.toFixed(2))}<br>Mediana (Q): ${Q.toFixed(2)}<br>----------------------------<br>`;
}
let simulacoes = [5, 50, 500, 1500, 3500, 5000, 10000, 50000,100000,10000000];
let resultDiv = document.getElementById('result');
simulacoes.forEach((n, i) => {
setTimeout(() => {
resultDiv.innerHTML += simularTED(n);
}, i * 500);
});
</script>
</body>
</html>