C# :: Aufgabe #84 :: Lösung #7

9 Lösungen Lösungen öffentlich
#84

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:

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%
#7
vote_ok
von n.rohde (400 Punkte) - 24.08.2015 um 13:43 Uhr
Quellcode ausblenden C#-Code
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();
        }
    }
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben