lotto zahlen -> rand() bekomme immer wieder die gleichen zahlen



  • Hallo, ich habe folgendes Problem:

    Ich habe ein kleines C++ Programm, dass durch die Funktion rand() mir Lottozahlen generieren soll.
    Aber wenn ich das Programm schließe und wieder öffne, erhalte ich immer die selben Zahlen.
    Warum?

    //-------------------------------------------------------------------------
    # include <iostream>
    # include <stdio.h>
    # include <conio.h>
    # include <stdlib.h>
    # include <time.h>
    # include <stdio.h>
    # include <string>
    # include <algorithm>
    # include <iomanip>
    using namespace std;
    //--------------Stephan Liebig 2006-----------------------------------------------------------------------
    
    # ifdef WIN32
    # ifdef __BORLANDC__					
    # include <conio.h>						
    # else
    
    void clrscr (void) {					
      system ("cls");
    }
    
    void pause (void) {
    	system ("PAUSE");
    }
    
    void beenden (void) {
    	system ("exit");
    }
    
    # endif
    # else /* WIN32 */						
    void clrscr(void) { }
    # endif
    
    /*
    	Das Programm generiert 6 Zahlen von 1 bis 49
    */
    
    const int KugelZahl = 6;
    const int MaxZahl = 49;
    
    int main()
    {
    	int lotto[KugelZahl];
    	int i;
    	int j;
    	int hilf;
    	bool neueZahl;
    
    	cout << "Lottozahlen: 6 aus 49!" << endl;
    	cout << "----------------------------------------------------" << endl;
    	cout << "Stephan Liebig" << endl << endl;
    
    		srand(38); // Mische einmal durch!
    		//Ermittle sechs Lottozahlen
    		for(i=0; i<KugelZahl; i++)
    		{
    			// Schleife prüft auf Doppelte
    			do
    			{
    				lotto[i] = rand() % MaxZahl + 1;
    				neueZahl = true; //positiv denken!
    				//Alle bisherigen Zahlen gegentesten
    				for(j=0; j<i; j++)
    				{
    					if(lotto[j]==lotto[i])
    					{
    						neueZahl = false; // die gab es schon!
    					}
    				}
    			}while (!neueZahl);
    		}
    		// Nun werden die Zahlen sortiert (per Bubblesort)
    		for(i=KugelZahl-1; i>0; i--)
    		{
    			for(j=0; j<i; j++)
    			{
    				if(lotto[j]>lotto[j+1])
    				{
    					hilf = lotto[j];
    					lotto[j] = lotto[j+1];
    					lotto[j+1] = hilf;
    				}
    			}
    		}
    		//Ausgabe der sortierten Zahlen
    		for(i=0; i<KugelZahl; i++)
    		{
    			cout << i+1 << ". Nummer: " << lotto[i] << endl;
    		}
    
    	getchar();
    	return 0;
    }
    


  • Hallo

    siehe hier.

    bis bald
    akari



  • srand(38); // Mische einmal durch!
    

    Du mischt nicht!

    #include <time.h>
    
    srand( ( unsigned int ) time( 0 ) );
    

    sollt's tun.

    Greetz, Swordfish



  • Funktioniert! Danke!


Anmelden zum Antworten