[C/C++] Zufallszahl C++ - Lotto

Dieses Thema im Forum "Programmierung & Entwicklung" wurde erstellt von Beogradjanin, 3. Juli 2008 .

Schlagworte:
Status des Themas:
Es sind keine weiteren Antworten möglich.
  1. 3. Juli 2008
    Zufallszahl C++ - Lotto

    Hi Leute,

    ich hab ein kleines Problem. Ich hab vorhin aus Spaß ein wenig rumgespielt und wollte ein Programm machen, dass mir 6 zufällige Lottozahlen ausgibt. Soweit so gut. Mein Problem ist nun, dass es auch passiert, das eine Zahl 2x erscheint.

    Als Bsp:

    Lottozahl 1: 5
    Lottozahl 2: 7
    Lottozahl 3: 7
    Lottozahl 4: 19
    Lottozahl 5: 30
    Lottozahl 6: 30

    Ich weiß, dass ich noch eine Unterabfrage, die die Zahlen auf Gleichheit überprüft, einbauen müsste. Nur mein Problem ist, dass es nicht bis jetzt geklappt hat.

    Füg mal den Code hier rein, würd mich über positve Antworten freuen!

    Code:
    
    #include <iostream.h>
    #include <time.h> 
    
    int main (void) 
    { 
     int zufallszahl[6];
     srand(time(NULL)); 
     cout << "Dieses Programm bestimmt ihre 6 Lottozahlen" << endl << endl; 
     
     for (int i=0; i<6; i++) 
     { 
     do
     {
     zufallszahl[i] = rand() % 49;
     
     } while (zufallszahl[i]==0);
     
     cout << "Lottozahl " << i+1 << " unsortiert: " << zufallszahl[i] << endl; 
     
     } 
     
     for (int x=0; x<6; x++)
     {
     for (int y=0; y<5; y++)
     { 
     if (zufallszahl[y] > zufallszahl[y+1])
     {
     int temp = zufallszahl[y];
     zufallszahl[y] = zufallszahl [y+1];
     zufallszahl[y+1] = temp;
     }
     }
     }
     
     
     cout << "------------------------------" << endl;
     
     for (int i=0; i<=5; i++) 
     { 
     cout << "Lottozahl " << i+1 << " sortiert: " << zufallszahl[i] << endl; 
     } 
     
     cout << endl; 
     
     
     system("pause");
     
     return 0; 
    }
    
    
    

    p.s. der Code jetzt ist ohne die weitere Unterabfrage!
     
  2. 3. Juli 2008
    AW: Zufallszahl C++ - Lotto

    Also die erste for-Schleife ist voellig unnoetig. Die Variable x benutzt du ja nirgends.
    Zu deinem anderen Problem würde ich beim erzeugen der Zufallszahl prüfen ob diese Zahl schon im Array ist( bevor du sie in das Array schreibst). Falls sie schon vorhanden ist einfach ne neue erzeugen.
    Gruß Jojo
     
  3. 3. Juli 2008
    AW: Zufallszahl C++ - Lotto

    hä? Wieso, das ist doch die Sortierung nach Bubblesort. Sortiere die Zahlen doch mit den beiden for-Schleifen nach der Größe (von klein -> groß) ?!
     
  4. 3. Juli 2008
    AW: Zufallszahl C++ - Lotto


    Code:
    for (int x=0; x<6; x++)
     {
     [COLOR=Magenta]for (int y=0; y<5; y++)
     { 
     if (zufallszahl[y] > zufallszahl[y+1])
     {
     int temp = zufallszahl[y];
     zufallszahl[y] = zufallszahl [y+1];
     zufallszahl[y+1] = temp;
     }
     }[/COLOR]
     }
    die obere for-schleife sorgt ja dafür, dass er die pinkte 6 mal macht.

    mfg
     
  5. 4. Juli 2008
    AW: Zufallszahl C++ - Lotto

    Les dir den Artikel mal durch, der bringt dich mit sicherheit weiter: c++.de :: Zufälle gibts?! - Funktionen rund um rand, Random und den Zufall
     
  6. 5. Juli 2008
    AW: Zufallszahl C++ - Lotto

    Code:
    do
     {
     zufallszahl[i] = rand() % 49;
     
     } while (zufallszahl[i]==0);
    da kannst du auch einfach
    Code:
    zufallszahl[i]=rand()%49+1;
    schreiben

    jez zum problem versteh ich richtig das du schwierigkeiten mit der prüfung hast ob die zahl schon mal "gezogen" wurde ?
     
  7. 8. Juli 2008
    AW: Zufallszahl C++ - Lotto

    ja, also ich kam bis jetzt nicht drauf, wie ich überprüfen kann, ob die zahl "bereits gezogen" wurde, und wenn ja, dann soll solange eine neue zahl gezogen werden, bis es keine doppelte gibt!

    also da das array von zufallszahl[5] insgesamt 6 zahlen zur verfügung stellt, würd ich halt gern 6 unterschiedliche zahlen ziehen, so dass es nicht vorkommt, dass z.b. eine zahl doppelt gezogen wird!!

    hab immernoch keine lösung
     
  8. 8. Juli 2008
    AW: Zufallszahl C++ - Lotto

    mit ner for und ner while schleife müsste das zu realisieren sein.. hab das auch schon ewig nicht mehr gemacht.. also quasi so:

    Code:
    zufallszahl[0] = rand()%49+1;
    for (int i=1; i < 6; i++)
    {
     bool nichtfertig = true;
     while (nichtfertig)
     {
     nichtfertig = false;
     zufallszahl[i] = rand()%49+1;
     for (int j =0; j < i; j++)
     if (zufallszahl[i] == zufallszahl[j])
     nichtfertig = true;
     }
    }
    hab das mal eben auf die schnelle so hingeschrieben.. müsste eigentlich laufen.. für syntaxfehler wird keine haftung übernommen
     
  9. 8. Juli 2008
    AW: Zufallszahl C++ - Lotto

    Code:
    #include <iostream>
    #include <time.h>
    using namespace std;
    
    int main () {
    
    
     int zufallszahl[6];
     
     cout << "Dieses Programm bestimmt ihre 6 Lottozahlen" << endl;
     int i =0;
    
    int zahlen[49];
    
    int r;
    while (i < 6)
    {
    cout << "bestimme zahl" << i+1 << endl;
    do {
     srand(time(NULL)); // ich persöhlich finde srand(time(NULL) * time(NULL)); besser bitte ausprobieren
     r = rand() % 49;
    
    
    
    }
    while (zahlen[r] == 1);
    zahlen[r] = 1;
    zufallszahl[i] = r+1;
    
    
    i++;
    
    
    }
    
     for (int i=0; i<=5; i++) 
     { 
     cout << "Lottozahl " << i+1 <<": "
     << zufallszahl[i] 
     << endl; 
     } 
    system("pause");
    return 0;
    }
    
    sollte gehen

    oder du testes jeden einzelnen speicherstand ab, ob er gleich ist

    und wenn man
    Code:
    srand(time(NULL));
    
    durch
    Code:
    srand(time(NULL)*time(NULL)); 
    ersetzt, dann is die rägelmäßigkeitr in den zahlen nich mehr ganz so zu erkennen und das ganze geht schella...


    wenn du alles sortieren willst und so kannste folgenden code übernehmen
    Code:
    #include <iostream>
    #include <time.h>
    using namespace std;
    
    int main () {
    
    
     int zufallszahl[6];
     
     cout << "Dieses Programm bestimmt ihre 6 Lottozahlen" << endl;
     int i =0;
    
    int zahlen[49];
    
    int r;
    while (i < 6)
    {
    cout << "bestimme zahl" << i+1 << endl;
    do {
     srand(time(NULL) * time(NULL)); 
     r = rand() % 49;
    
    
    
    }
    while (zahlen[r] == 1);
    zahlen[r] = 1;
    zufallszahl[i] = r+1;
    
    
    i++;
    
    
    }
    cout <<"unsortiert:" <<endl;
     for (int i=0; i<=5; i++) 
     { 
     cout << "Lottozahl " << i+1 <<": "
     << zufallszahl[i] 
     << endl; 
     } 
    
    cout << "________________________________________________________________________________ \n"
     << "sortiert: "<< endl;
     for (int x=0; x<6; x++)
     {
     for (int y=0; y<5; y++)
     { 
     if (zufallszahl[y] > zufallszahl[y+1])
     {
     int temp = zufallszahl[y];
     zufallszahl[y] = zufallszahl [y+1];
     zufallszahl[y+1] = temp;
     }
     
    }
    }
     for (int i=0; i<=5; i++) 
     { 
     cout << "Lottozahl " << i+1 <<": "
     << zufallszahl[i] 
     << endl; 
     } 
    system("pause");
    return 0;
    }
    
    wenn man nicht srand(time(NULL) * time(NULL)); benutzt, dann ist das sortierte fast imma gleich dem unsortierten oda es fängt nach 49 zb wieda bei 3 an
     
  10. 9. Juli 2008
    AW: Zufallszahl C++ - Lotto

    vielen DANK an euch beide!!! es hat geklappt!!

    habt ne BW bekommen!!

    ~CLOSED~
     
  11. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.