C# :: Aufgabe #84
9 Lösungen

Verteilung beim Würfeln mit zwei Würfeln
Anfänger - C#
von Alucard1766
- 11.02.2015 um 13:59 Uhr
Angenommen du wirfst sehr viel mal mit zwei Würfeln. Wie sieht die Verteilung der Summe der geworfenen Würfelzahlen aus?
Vorgaben:
Simuliere möglichst zufällig sehr viele Würfe mit zwei Standardwürfel(=1-6).
Mindestens 100'000 Würfe, je mehr desto besser. -> Performanter Code
Gib aus, wie die Verteilung der Summe der beiden Würfeln prozentual/im Verhältnis aussehen
Du musst in der Lösung auf dieser Seite eine gut sicht- und interpretierbare Darstellung der Verteilung haben, damit wir die Ergebnisse vergleichen können
Beispiel:
Vorgaben:
Simuliere möglichst zufällig sehr viele Würfe mit zwei Standardwürfel(=1-6).
Mindestens 100'000 Würfe, je mehr desto besser. -> Performanter Code
Gib aus, wie die Verteilung der Summe der beiden Würfeln prozentual/im Verhältnis aussehen
Du musst in der Lösung auf dieser Seite eine gut sicht- und interpretierbare Darstellung der Verteilung haben, damit wir die Ergebnisse vergleichen können
Beispiel:
Konsolenausgabe:
1. Wurf: 1 und 5 - Summe 6
2. Wurf: 4 und 2 - Summe 6
...
100000. Wurf: 3 und 1 - Summe 4
Ergebnis:
1 - 5%
2 - 7%
12 - 6%
Lösungen:
Was kann man denn bei der Performance großartig falsch machen?
C#-Code

using System; namespace trainYourProgrammer { class MainClass { private static Random random = new Random(); private static int ThrowDice() { return 2 + random.Next (6) + random.Next (6); } public static void Main(string[] args) { int[] count = new int[13]; double limit = 1e8; for (int i = (int)limit; i > 0; i--) count [ThrowDice ()]++; for (int i = 2; i <= 12; i++) Console.WriteLine ("{0,2} - {1,6:P}", i, count [i] / limit); } } } /* 2 - 2,78% 3 - 5,55% 4 - 8,34% 5 - 11,11% 6 - 13,88% 7 - 16,67% 8 - 13,89% 9 - 11,11% 10 - 8,33% 11 - 5,55% 12 - 2,78% */
Auf vier Threads parallelisiert, auch wenn ich nicht glaube dass das wirklich einen Unterschied macht. Die Ausgabe auf der Konsole killt dann eh wieder alles weil die viel zu langsam ist. Ist auf 1 Mio. eingestellt, kann aber ganz leicht in int würfegesamt geändert werden.
Wie kam der Ersteller eigentlich auf 1 - 5 Prozent ? Eine 1 dürfte nie rauskommen, bei zwei Würfeln.
C#-Code
Wie kam der Ersteller eigentlich auf 1 - 5 Prozent ? Eine 1 dürfte nie rauskommen, bei zwei Würfeln.

using System; using System.Collections.Generic; namespace Würfel { class Program { static int würfegesamt = 1000000; static void Main(string[] args) { Console.Write("{0:#,#} mal würfeln lassen?", würfegesamt); Console.ReadKey(true); List<Würfe> Threads = new List<Würfe>(); for (int i = 0; i < 4; i++) Threads.Add(new Würfe()); System.Threading.Tasks.Parallel.ForEach(Threads, x => x.Würfeln()); Console.WriteLine("\n\nWürfeln abgeschlossen, Ausgabe?\n-Überspringen mit Esc"); if (((ConsoleKeyInfo)Console.ReadKey(true)).Key != ConsoleKey.Escape) { for (int i = 0; i < Threads.Count; i++) for (int j = 0; j < Threads[i].AlleWürfe.Count; j++) Console.WriteLine("{0,9:#,#}. Wurf: {1} und {2} - Summe {3,2}", (i * (würfegesamt / 4)) + (j + 1), Threads[i].AlleWürfe[j].Wurf[0], Threads[i].AlleWürfe[j].Wurf[1], Threads[i].AlleWürfe[j].Summe); } Console.WriteLine("\nDurchschnitt berechnen!"); Console.ReadKey(false); int[] anteile = new int[11]; for (int i = 0; i < anteile.Length; i++) anteile[i] = 0; for (int i = 0; i < Threads.Count; i++) for (int j = 0; j < Threads[i].AlleWürfe.Count; j++) anteile[Threads[i].AlleWürfe[j].Summe - 2]++; for (int i = 0; i < anteile.Length; i++) Console.WriteLine("{0,2} - {1,3:0.0}%", i + 2, (100 / (double)würfegesamt) * anteile[i]); Console.ReadKey(true); } class Würfe { public List<EinWurf> AlleWürfe = new List<EinWurf>(); public void Würfeln() { Random rnd = new Random(); for (int i = 0; i < würfegesamt / 4; i++) this.AlleWürfe.Add(new EinWurf(rnd.Next(1, 7), rnd.Next(1, 7))); } } class EinWurf { byte[] wurf = new byte[2]; public byte[] Wurf { get { return wurf; } set { wurf = value; } } byte summe; public byte Summe { get { return summe; } set { summe = value; } } public EinWurf(int wurf1, int wurf2) { wurf[0] = (byte)wurf1; wurf[1] = (byte)wurf2; summe = (byte)(wurf1 + wurf2); } } } }

using System; using System.Collections.Generic; using System.Linq; namespace SymTech { class Program { static Random r = new Random(); static void Main(string[] args) { List<int> summen = new List<int>(); int würfel1 = 0; int würfel2 = 0; float durchläufe = 1000000; for (int i = 0; i < durchläufe; i++) { würfel1 = r.Next(1, 7); würfel2 = r.Next(1, 7); summen.Add(würfel1 + würfel2); Console.WriteLine((i + 1) + ". Wurf: " + würfel1 + " und " + würfel2 + " - Summe " + summen[i]); } Console.WriteLine(); Console.WriteLine("Ergebnis:"); for (int i = 1; i <= 12; i++) { Console.WriteLine(i.ToString().PadRight(3) + "- " + ((summen.Count(p => p == i) * 100) / durchläufe).ToString("0.000").PadLeft(6) + "%"); } Console.ReadKey(); } } }

static void Main(string[] args) { ushort[] Vorkommen = new ushort[11]; Random Rndm = new Random(); for (int i = 0; i < 100000; i++) Vorkommen[Rndm.Next(6) + Rndm.Next(6)]++; for (int i = 0; i < Vorkommen.Length; i++) Console.WriteLine("{0} wurde {1} mal geworfen. {2}%", i + 2, Vorkommen[i], Vorkommen[i] / 1000f); Console.Read(); }
Konsolenausgabe:
2 wurde 2829 mal geworfen. 2,829%
3 wurde 5575 mal geworfen. 5,575%
4 wurde 8359 mal geworfen. 8,359%
5 wurde 11112 mal geworfen. 11,112%
6 wurde 13828 mal geworfen. 13,828%
7 wurde 16751 mal geworfen. 16,751%
8 wurde 13806 mal geworfen. 13,806%
9 wurde 11186 mal geworfen. 11,186%
10 wurde 8296 mal geworfen. 8,296%
11 wurde 5427 mal geworfen. 5,427%
12 wurde 2831 mal geworfen. 2,831%

static void Main(string[] args) { Random random = new Random(); int Würfel1, Würfel2; int[] Summen = new int[13]; for (int i = 1; i < 100001; i++) { Console.WriteLine(i + ". Wurf: " + (Würfel1 = random.Next(1, 7)) + " und " + (Würfel2 = random.Next(1, 7)) + " - Summe " + (Würfel1 + Würfel2)); Summen[Würfel1 + Würfel2]++; } Console.WriteLine(); for (int i = 2; i < 13; i++) Console.WriteLine(i + " wurde " + Summen[i] + "-Mal geworfen. \t" + (Summen[i] * 100 / 100000f) + "%"); Console.ReadLine(); }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Wuerfelwurf { class Program { static void Main(string[] args) { long number; do{ Console.WriteLine("Wie oft soll geworfen werden ?"); }while(!long.TryParse(Console.ReadLine(), out number)); Random seed = new Random(265); Random wuerfel1 = new Random(seed.Next()); Random wuerfel2 = new Random(seed.Next()); long[] verteilung = new long[11]; int zahl1, zahl2; for (long i = 0; i < number; i++) { zahl1 = wuerfel1.Next(1, 7); zahl2 = wuerfel2.Next(1, 7); for (int j = 0; j < 11; j++) { if ((zahl1 + zahl2) == (j + 2)) { verteilung[j]++; break; } } } Console.Clear(); // Verteilung ausgeben: for (int i = 0; i < 11; i++) { decimal prozente = ((decimal)verteilung[i] / (decimal)number) * (decimal)100; Console.WriteLine("Zahl: {0,2}, Verteilung: {1}%", i + 2, prozente); } Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Verteilung_beim_Würfeln_mit_zwei_Würfel { class Program { static void Main(string[] args) { int wuerfel1, wuerfel2, anzahldurchlaeufe = 100000; int anzahlSumme2 = 0, anzahlSumme3 = 0, anzahlSumme4 = 0, anzahlSumme5 = 0, anzahlSumme6 = 0, anzahlSumme7 = 0, anzahlSumme8 = 0, anzahlSumme9 = 0, anzahlSumme10 = 0, anzahlSumme11 = 0, anzahlSumme12 = 0; for (int i = 0; i < anzahldurchlaeufe; i++) { Random zufallszahlen = new Random(); System.Threading.Thread.Sleep(50); wuerfel1 = zufallszahlen.Next(1, 7); wuerfel2 = zufallszahlen.Next(1, 7); Console.WriteLine(Convert.ToInt32(i + 1) + " Wurf: " + Convert.ToInt32(wuerfel1) + " und " + Convert.ToInt32(wuerfel2) + " - Summe " + Convert.ToInt32(wuerfel1 + wuerfel2)); if ((wuerfel1 + wuerfel2) == 2) anzahlSumme2++; else if ((wuerfel1 + wuerfel2) == 3) anzahlSumme3++; else if ((wuerfel1 + wuerfel2) == 4) anzahlSumme4++; else if ((wuerfel1 + wuerfel2) == 5) anzahlSumme5++; else if ((wuerfel1 + wuerfel2) == 6) anzahlSumme6++; else if ((wuerfel1 + wuerfel2) == 7) anzahlSumme7++; else if ((wuerfel1 + wuerfel2) == 8) anzahlSumme8++; else if ((wuerfel1 + wuerfel2) == 9) anzahlSumme9++; else if ((wuerfel1 + wuerfel2) == 10) anzahlSumme10++; else if ((wuerfel1 + wuerfel2) == 11) anzahlSumme11++; else if ((wuerfel1 + wuerfel2) == 12) anzahlSumme12++; } Console.WriteLine("Ergebnis:"); Console.WriteLine("2\t- " + Convert.ToString(Convert.ToDouble(anzahlSumme2) / anzahldurchlaeufe * 100) + "%"); Console.WriteLine("3\t- " + Convert.ToString(Convert.ToDouble(anzahlSumme3) / anzahldurchlaeufe * 100) + "%"); Console.WriteLine("4\t- " + Convert.ToString(Convert.ToDouble(anzahlSumme4) / anzahldurchlaeufe * 100) + "%"); Console.WriteLine("5\t- " + Convert.ToString(Convert.ToDouble(anzahlSumme5) / anzahldurchlaeufe * 100) + "%"); Console.WriteLine("6\t- " + Convert.ToString(Convert.ToDouble(anzahlSumme6) / anzahldurchlaeufe * 100) + "%"); Console.WriteLine("7\t- " + Convert.ToString(Convert.ToDouble(anzahlSumme7) / anzahldurchlaeufe * 100) + "%"); Console.WriteLine("8\t- " + Convert.ToString(Convert.ToDouble(anzahlSumme8) / anzahldurchlaeufe * 100) + "%"); Console.WriteLine("9\t- " + Convert.ToString(Convert.ToDouble(anzahlSumme9) / anzahldurchlaeufe * 100) + "%"); Console.WriteLine("10\t- " + Convert.ToString(Convert.ToDouble(anzahlSumme10) / anzahldurchlaeufe * 100) + "%"); Console.WriteLine("11\t- " + Convert.ToString(Convert.ToDouble(anzahlSumme11) / anzahldurchlaeufe * 100) + "%"); Console.WriteLine("12\t- " + Convert.ToString(Convert.ToDouble(anzahlSumme12) / anzahldurchlaeufe * 100) + "%"); Console.Read(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TrainYourProgrammer84 { class Program { static void Main(string[] args) { int anzahlWürfe = 0; int zaehler = 0, zahlEins = 0, zahlZwei = 0; double zwei = 0, drei = 0, vier = 0, fuenf = 0, sechs = 0, sieben = 0, acht = 0, neun = 0, zehn = 0, elf = 0, zwoelf = 0; Console.WriteLine("Würfelsimulation"); Console.WriteLine("Geben Sie an, wie oft geworfen werden soll: "); while (!Int32.TryParse(Console.ReadLine(), out anzahlWürfe)) { Console.WriteLine("Geben Sie an, wie oft geworfen werden soll: "); } Random zufall = new Random(); while (zaehler < anzahlWürfe) { zahlEins = zufall.Next(1, 7); zahlZwei = zufall.Next(1, 7); Console.WriteLine("{0}. Wurf: {1} und {2} - Summe {3}", zaehler + 1, zahlEins, zahlZwei, zahlEins + zahlZwei); switch (zahlEins + zahlZwei) { case 2: zwei++; break; case 3: drei++; break; case 4: vier++; break; case 5: fuenf++; break; case 6: sechs++; break; case 7: sieben++; break; case 8: acht++; break; case 9: neun++; break; case 10: zehn++; break; case 11: elf++; break; case 12: zwoelf++; break; } zaehler++; } zwei = zwei / anzahlWürfe *100; drei = drei / anzahlWürfe * 100; vier = vier / anzahlWürfe * 100; fuenf = fuenf / anzahlWürfe * 100; sechs = sechs / anzahlWürfe * 100; sieben = sieben / anzahlWürfe * 100; acht = acht / anzahlWürfe * 100; neun = neun / anzahlWürfe * 100; zehn = zehn / anzahlWürfe * 100; elf = elf / anzahlWürfe * 100; zwoelf = zwoelf / anzahlWürfe * 100; Console.WriteLine("Ergebnis:"); Console.WriteLine("2 - {0:F2}%", zwei); Console.WriteLine("3 - {0:F2}%", drei); Console.WriteLine("4 - {0:F2}%", vier); Console.WriteLine("5 - {0:F2}%", fuenf); Console.WriteLine("6 - {0:F2}%", sechs); Console.WriteLine("7 - {0:F2}%", sieben); Console.WriteLine("8 - {0:F2}%", acht); Console.WriteLine("9 - {0:F2}%", neun); Console.WriteLine("10 - {0:F2}%", zehn); Console.WriteLine("11 - {0:F2}%", elf); Console.WriteLine("12 - {0:F2}%", zwoelf); Console.ReadKey(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace würfeln { class Program { static void Main(string[] args) { Random zufall = new Random(); int[] sum = new int[13]; for(int i = 0; i <= 100000; i++) { int würfel1 = Convert.ToInt32(zufall.Next(1, 7)); int würfel2 = Convert.ToInt32(zufall.Next(1, 7)); int summe = würfel1 + würfel2; sum[würfel1 + würfel2]++; } for (int i = 2; i < 13; i++) Console.WriteLine(i + " wurde " + sum[i] + " Mal geworfen. Das entspricht: " + (sum[i] * 100 / 100000f) + "%"); Console.ReadLine(); Console.ReadKey(); } } }