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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| package main
import (
"fmt"
"math"
"math/rand"
"strconv"
"strings"
"time"
)
const nn28 int = 28
const nn114 int = 114
type solution struct {
tb [nn28]int
}
func (s solution) calcule_somme() int {
ssom := 0
for i := 0; i < nn28; i++ {
ssom += s.tb[i]
}
return ssom
}
func init00() []int {
var ch string
var tb1 []int
ch = "286,227,206,200,182,176,165,135,129,128,123,120,118,112,111,111,110,109,99,98,96,93,89,88,88,85,83,78,78,77,75,75,73,69,64,62,60,60,59,56,55,54,54,53,52,52,52,50,49,46,45,45,44,43,42,40,40,38,37,36,35,34,31,30,30,30,29,29,29,28,28,26,25,24,22,22,21,20,20,19,19,19,18,18,17,15,14,13,12,12,11,11,11,11,11,9,8,8,8,8,8,7,7,6,6,5,5,5,5,4,4,3,3,3"
stringSlice := strings.Split(ch, ",")
for i := 0; i < nn114; i++ {
j, _ := strconv.Atoi(stringSlice[i])
tb1 = append(tb1, j)
}
return tb1
}
func slice_remove(s []int, i int) []int {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}
func f_random(tb0 []int, r *rand.Rand) solution {
//var tb1 []int
//var nn int
var ssol solution
tb1 := make([]int, len(tb0))
copy(tb1, tb0)
nntot := len(tb1)
for i := 0; i < nn28; i++ {
i1 := r.Intn(nntot)
ssol.tb[i] = tb1[i1]
tb1 = slice_remove(tb1, i1)
nntot = nntot - 1
}
return ssol
}
func main9(tb0 []int, r *rand.Rand) {
var ssol solution
var cpt [10000]int // la somme ne devrait jamais dépasser 10000 ... je prévois très large !
var Million int = 100000000
fmt.Println("Nouvelle simulation, nb runs=", Million)
for iii := 0; iii < Million; iii++ {
ssol = f_random(tb0, r)
ssum := ssol.calcule_somme()
cpt[ssum] = cpt[ssum] + 1
}
ss3 := 0
if cpt[1105] > 0 {
fmt.Println("Fréquence de 1105= 1/", Million/cpt[1105])
}
best_ii2 := 0
best0 := 0
mediane_vue := 0
stot := 0
for ii2 := 0; ii2 < 10000; ii2++ {
if cpt[ii2] > 0 {
ss3 += cpt[ii2]
stot += cpt[ii2] * ii2
// fmt.Println(ii2, cpt[ii2], ss3)
if ss3*2 > Million {
if mediane_vue == 0 {
fmt.Println(" Médiane =", ii2)
mediane_vue = 1
}
}
if cpt[ii2] > best0 {
best0 = cpt[ii2]
best_ii2 = ii2
}
}
}
fmt.Println(" Valeur modale =", best_ii2, " fréquence= 1/", Million/best0)
fmt.Println(" Valeur moyenne = ", stot/Million)
}
func main() {
var tb0 []int
r := rand.New(rand.NewSource(time.Now().UnixNano()))
tb0 = init00()
for ttt := 0; ttt < 5; ttt++ {
main9(tb0, r)
}
} |
Partager