C# :: Aufgabe #258 :: Lösung #6

7 Lösungen Lösungen öffentlich
#258

Stochastische Ermittlung der Eulerschen-Zahl

Anfänger - C# von hollst - 12.04.2019 um 09:37 Uhr
Wir betrachten folgendes Zufallsexperiment: Aus einer Menge reeller und gleichverteilter Zahlen {X} mit 0 <= x < 1.0
ziehen wir solange Zahlen x1, x2 ... xn bis deren Summe >= 1.0 ist. n ist dann das Ergebnis eines Zufallsexperimentes.
Wir müssen mindestens zweimal ziehen, den ein einzelnes Zufalls-x ist ja immer kleiner als 1.0.

Die Frage lautet: Wie groß ist n im Mittel?

Hinweis: Laut Theorie ist n = 2.718281828459045... (= e). Nun gut, grau ist alle Theorie, wir wollen sehen, ob der Computer
(in etwa) der gleichen Meinung ist.

Viel Spaß!
#6
vote_ok
von Waldgeist (2310 Punkte) - 04.11.2019 um 19:12 Uhr
Ich habe es als Windows-Form App geschrieben:

Form1.cs

Quellcode ausblenden C#-Code
using System;
using System.Windows.Forms;

namespace Aufgabe_258
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            double Wiederholung = Convert.ToDouble(Wiederholungen.Text);
            int zaehler = 1;


            Random Zufall = new Random();

            for (int i = 1; i <= Wiederholung; i++)
            {
                double summe = 0;
                do
                {

                    double x = Zufall.NextDouble();
                    summe = summe + x;
                    zaehler++;
                } while (summe <= 1.0);


            }
            Funktionswert.Text = Convert.ToString(Math.E);
            Ausgabe.Text = Convert.ToString(zaehler / Wiederholung);
            Differenz.Text = Convert.ToString(Math.E - zaehler / Wiederholung);
        }

    }
}




Form1.Designer.cs

Quellcode ausblenden C#-Code
namespace Aufgabe_258
{
    partial class Form1
    {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Verwendete Ressourcen bereinigen.
        /// </summary>
        /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Vom Windows Form-Designer generierter Code

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.Wiederholungen = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.Ausgabe = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.Funktionswert = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.Differenz = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(68, 100);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(85, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Wiederholungen";
            // 
            // Wiederholungen
            // 
            this.Wiederholungen.Location = new System.Drawing.Point(179, 93);
            this.Wiederholungen.Name = "Wiederholungen";
            this.Wiederholungen.Size = new System.Drawing.Size(182, 20);
            this.Wiederholungen.TabIndex = 1;
            this.Wiederholungen.Text = "10";
            this.Wiederholungen.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(179, 150);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(182, 43);
            this.button1.TabIndex = 2;
            this.button1.Text = "Berechnen";
            this.button1.UseVisualStyleBackColor = false;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(630, 388);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(156, 51);
            this.button2.TabIndex = 3;
            this.button2.Text = "Beenden";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(62, 269);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(91, 13);
            this.label2.TabIndex = 4;
            this.label2.Text = "Berechneter Wert";
            // 
            // Ausgabe
            // 
            this.Ausgabe.Location = new System.Drawing.Point(178, 262);
            this.Ausgabe.Name = "Ausgabe";
            this.Ausgabe.ReadOnly = true;
            this.Ausgabe.Size = new System.Drawing.Size(183, 20);
            this.Ausgabe.TabIndex = 5;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(55, 329);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(98, 13);
            this.label3.TabIndex = 6;
            this.label3.Text = "Theoretischer Wert";
            // 
            // Funktionswert
            // 
            this.Funktionswert.Location = new System.Drawing.Point(178, 322);
            this.Funktionswert.Name = "Funktionswert";
            this.Funktionswert.ReadOnly = true;
            this.Funktionswert.Size = new System.Drawing.Size(183, 20);
            this.Funktionswert.TabIndex = 7;
            this.Funktionswert.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(104, 371);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(49, 13);
            this.label4.TabIndex = 8;
            this.label4.Text = "Differenz";
            // 
            // Differenz
            // 
            this.Differenz.Location = new System.Drawing.Point(179, 364);
            this.Differenz.Name = "Differenz";
            this.Differenz.ReadOnly = true;
            this.Differenz.Size = new System.Drawing.Size(182, 20);
            this.Differenz.TabIndex = 9;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.Differenz);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.Funktionswert);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.Ausgabe);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.Wiederholungen);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Aufgabe 258 / Ermittlung der Eulerschen Zahl";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox Wiederholungen;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox Ausgabe;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox Funktionswert;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox Differenz;
    }
}


Kommentare:

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

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