Guest User

Untitled

a guest
Jan 17th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. <?php
  2.  
  3. // effectuer un tirage au sort (simulation d'un individu)
  4. function trial()
  5. {
  6. // probabilité de croyance de chaque théorie d'après l'enquête
  7. static $probs = [0.55, 0.54, 0.32, 0.31, 0.28, 0.24, 0.20, 0.18, 0.17, 0.16, 0.09];
  8. $count = 0;
  9. // pour chaque théorie, on tire au sort conformément à sa prob. si l'individu
  10. // y adhere ou non
  11. foreach($probs as $p)
  12. {
  13. if((float)rand()/(float)getrandmax() < $p)
  14. $count++;
  15. }
  16. return $count;
  17. }
  18.  
  19. // on simule un grand nombre d'individus pour tendre vers la vérité
  20. // (erreur stat totalement négligeable)
  21. $trials = 10000000;
  22.  
  23. // init
  24. $pop = [];
  25. for($i = 0; $i <= 11; ++$i)
  26. {
  27. $pop[$i] = 0;
  28. }
  29.  
  30. // essais
  31. for($i = 0; $i < $trials; ++$i)
  32. {
  33. $pop[trial()]++;
  34. }
  35.  
  36. // normalisation
  37. $sum = array_sum($pop);
  38. for($i = 0; $i <= 11; ++$i)
  39. {
  40. $pop[$i] /= $sum/100;
  41. }
  42.  
  43. // affichage
  44.  
  45. for($i = 0; $i <= 4; ++$i)
  46. {
  47. echo "$i {$pop[$i]}\n";
  48. }
  49. $fs = $pop[5]+$pop[6];
  50. echo "5,6 $fs\n";
  51. $s = 0;
  52. for($i = 7; $i <= 11; ++$i)
  53. {
  54. $s += $pop[$i];
  55. }
  56. echo "7+ $s";
Add Comment
Please, Sign In to add comment