Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[C#] Blackjack - Randomzahlen identisch

Empfohlene Antworten

Veröffentlicht

Hallo,

ich versuche gerade mir ein recht simples BlackJack-Spiel zu schreiben. Allerdings sind die mit Random generierten Zahlen beim Klick auf den Start Button identisch.

      
private void btnStart_Click(object sender, EventArgs e)
{
label4.Text = "";
label5.Text = "";
label6.Text = "";

rdn1();
rdn2();

txtAugenGesamt.Text = (wert1 + wert2).ToString();
btnStart.Visible = false;
}

private void rdn1()
{
Random augen1 = new Random();
wert1 = augen1.Next(1, 12);
label2.Text = wert1.ToString();
}

private void rdn2()
{
Random augen2 = new Random();
wert2 = augen2.Next(1, 12);
label3.Text = wert2.ToString();
}
[/PHP]

Setze ich einen Haltepunkt bei "Random augen1 ..." erhalte ich ebenso identische Zahlen. Setze ich jedoch einen weiteren bei "Random augen2 ..." erhalte ich unterschiedliche Zahlen.

Gibt es hierfür eine sinnvolle Erklärung warum mein Programm keine Lust hat eine weitere Zahl zu generieren sondern einfach nochmal die erste nimmt?

mfg, Stefan

Generatoren für Zufallszahlen sind - wenn nicht mit spezieller Hardware - immer mehr oder weniger unzufällig.

In Deinem speziellen Fall würde ich mal testen:


            Random augen = new Random(); // als static property


private void btnStart_Click(object sender, EventArgs e) 

        { 

            label4.Text = ""; 

            label5.Text = ""; 

            label6.Text = ""; 


            rdn1(); 

            rdn2(); 


            txtAugenGesamt.Text = (wert1 + wert2).ToString(); 

            btnStart.Visible = false; 

        } 


        private void rdn1() 

        { 

            wert1 = augen.Next(1, 12); 

            label2.Text = wert1.ToString(); 

        } 


        private void rdn2() 

        { 

            wert2 = augen.Next(1, 12); 

            label3.Text = wert2.ToString(); 

        } 

vielen dank.

das hat funktioniert. :)

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.