Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Labo_09
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. LireValeur(out int NB_LANCERS);
  14.  
  15. const int NOMBRE_DÉS = 2;
  16. const int NOMBRE_FACES = 6;
  17. // Console.WriteLine($"pour {NOMBRE_DÉS} dés à {NOMBRE_FACES} faces les probabilités sont : ");
  18. int[] tableauDés = CréerDistribution(NOMBRE_DÉS, NOMBRE_FACES);
  19.  
  20. Random générateur = new Random();
  21. SimulerLancerDesDés(NOMBRE_DÉS, NOMBRE_FACES, tableauDés, NB_LANCERS, ref générateur);
  22. // Console.WriteLine(tableauDés[0]);
  23. Console.WriteLine($"pour {NOMBRE_DÉS} dés à {NOMBRE_FACES} faces les probabilités sont : ");
  24.  
  25. AfficherDistribution(tableauDés, NB_LANCERS);
  26.  
  27.  
  28.  
  29.  
  30.  
  31. }
  32. static int [] CréerDistribution( int nombreDés, int nombreFaces)
  33. {
  34. int indiceTab;
  35. indiceTab = nombreDés * nombreFaces - nombreDés + 1;
  36. int[] tableau;
  37. tableau = new int[indiceTab];
  38. return tableau;
  39. }
  40.  
  41. static void LireValeur(out int NB_LANCERS)
  42. {
  43. const int NB_LANCERS_MIN = 500;
  44. const int NB_LANCERS_MAX = 10000;
  45. bool succes /*= false*/;
  46.  
  47. Console.WriteLine("Quel est le nombre de lancers que vous voulez effectuer ([500,10000]) : ");
  48.  
  49. //NB_LANCERS = int.Parse(Console.ReadLine());
  50. succes = int.TryParse(Console.ReadLine(), out NB_LANCERS);
  51.  
  52. //Console.WriteLine(succes);
  53. while ((NB_LANCERS < NB_LANCERS_MIN || NB_LANCERS > NB_LANCERS_MAX) || !succes)
  54. {
  55. Console.WriteLine("erreur la valeur doit etre entre 500 et 10000 inclusivement.");
  56. Console.WriteLine(String.Concat(Enumerable.Repeat("-", 70)));
  57. Console.WriteLine("Quel est le nombre de lancers que vous voulez effectuer ([500,10000]) : ");
  58. succes = int.TryParse(Console.ReadLine(), out NB_LANCERS);
  59. // NB_LANCERS = int.Parse(Console.ReadLine());
  60.  
  61. }
  62.  
  63. // return NB_LANCERS;
  64.  
  65. }
  66. static void SimulerLancerDesDés(int NOMBRE_DÉS, int NOMBRE_FACES, int[] tableauDés, int NB_LANCERS, ref Random générateur)
  67. {
  68. //int VALEUR_MAX_DÉS = NOMBRE_FACES * NOMBRE_DÉS;
  69. //bool Valeur = false;
  70. //if (!(Valeur))
  71. //{
  72. int valeurAléatoireFinale;
  73. for (int compteur = 0; compteur < NB_LANCERS; compteur++)
  74. {
  75. //Valeur = true;
  76. valeurAléatoireFinale = RoulerDés(NOMBRE_DÉS, NOMBRE_FACES, ref générateur);
  77. // Console.WriteLine(valeurAléatoireFinale);
  78.  
  79. //introduireDansTab(valeurAléatoireFinale, tableauDés, NOMBRE_DÉS);
  80. int indiceTab = valeurAléatoireFinale - NOMBRE_DÉS;
  81. tableauDés[indiceTab] = tableauDés[indiceTab] + 1;
  82.  
  83. }
  84. //}
  85. }
  86.  
  87. static int RoulerDés(int NOMBRE_DÉS, int NOMBRE_FACES, ref Random générateur)
  88.  
  89. {
  90. // int VALEUR_MAX_DÉS = (NOMBRE_FACES * NOMBRE_DÉS);
  91. int valeurAléatoire1 = RoulerDé(NOMBRE_FACES, NOMBRE_DÉS, ref générateur);
  92. int valeurAléatoire2 = RoulerDé(NOMBRE_FACES, NOMBRE_DÉS, ref générateur);
  93. int valeurAléatoireFinale = valeurAléatoire1 + valeurAléatoire2;
  94. return valeurAléatoireFinale;
  95. //for (int compteur = 0; tableauDés[compteur] < tableauDés.Length; compteur++)
  96. //{
  97. // int valeurAléatoire = générateur.Next(NOMBRE_DÉS, VALEUR_MAX_DÉS);
  98. // valeurAléatoire
  99.  
  100. //}
  101.  
  102.  
  103. //static void SimulerLancerDesDés(int NOMBRE_DÉS, int NOMBRE_FACES, int[] tableauDés, Random générateur)
  104. //{
  105. // int VALEUR_MAX_DÉS = NOMBRE_FACES * NOMBRE_DÉS;
  106. // for (int compteur = 0; tableauDés[compteur] < tableauDés.Length; compteur++)
  107. // {
  108. // int valeurAléatoire = générateur.Next(NOMBRE_DÉS, VALEUR_MAX_DÉS);
  109. // valeurAléatoire
  110.  
  111. // }
  112.  
  113. }
  114.  
  115.  
  116.  
  117.  
  118. static int RoulerDé(int VALEUR_MAX_DÉS, int NOMBRE_DÉS, ref Random générateur)
  119. {
  120. int valeurAléatoire = générateur.Next(NOMBRE_DÉS-1, VALEUR_MAX_DÉS+1);
  121. return valeurAléatoire;
  122. }
  123. //static void introduireDansTab(int valeurAléatoireFinale, int[] tableau, int NOMBRE_DÉS)
  124. //{
  125. // for (int compteur = 0; compteur < tableau.Length; compteur++)
  126. // {
  127. // // int indiceTab = 0;
  128. // int indiceTab = valeurAléatoireFinale - NOMBRE_DÉS;
  129. // tableau[indiceTab] = tableau[indiceTab] + 1;
  130. // }
  131. //}
  132. static void AfficherDistribution(int[] tableau, int NB_LANCERS)
  133. {
  134. int indiceTab = 0;
  135. int somme = 2;
  136. for(int compteur = 0; compteur < tableau.Length; compteur++)
  137. {
  138. int MAX = 100;
  139.  
  140. double pourcent;
  141. pourcent = (double)MAX * ( tableau[indiceTab]) / NB_LANCERS;
  142. Console.WriteLine($"Somme des dés : {somme}, proportion {pourcent:f2}%:{CréerBarreHistogramme(MAX, pourcent)} ");
  143. ++indiceTab;
  144. ++somme;
  145. }
  146. }
  147. static string CréerBarreHistogramme(int max, double proportion)
  148. {
  149. // on pourrait également le faire avec une boucle for... :/
  150. return new string('#', (int)Math.Round(
  151. ( proportion), 0, MidpointRounding.AwayFromZero));
  152. }
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement