Zufallsgenerator..



  • Hallo!

    Mir ist es soweit gelungen, einen einiger Maßen funktionierenden Zufallsgenerator zu programmieren!

    #include <iostream>
    #include <windows.h>
    #include <fstream>
    #pragma comment (lib, "winmm.lib")
    using namespace std;
    
    int main ()
    {
    	srand (timeGetTime());
    	long int Name = (rand()%3000)+1 * 634968 * 32095 * 5698 * 29478;
    
    	ofstream out ("Name.txt", ios_base::app);
    
    	out << Name << endl;
    
    	out.close ();
    
    	return 0;
    }
    

    Jetzt werden relativ große Zufallszahlen erstellt. Der Haken ist, dass sie negativ sind, trotz long. Wie geht das anders? Also sie sollten prositiv sein.

    Dann wollte ich noch, dass die Datei ebenfalls einen Zufallsnamen bekommt. Aber ich weiß einfach nicht wie 😞 "Name.txt" klappt auf jeden fall nicht xD

    Danke im Voraus!
    MfG Darthshoot!



  • Hallo

    mal -1 rechnen.

    chrische



  • darthshoot nicht logged schrieb:

    long int Name = (rand()%3000)+1 * 634968 * 32095 * 5698 * 29478;
    

    Was hat das für nen Sinn(* 634968 * 32095 * 5698 * 29478)???

    PS. Gut das du nicht gelogged wirst.



  • Der Sinn ist, dass die Zufallszahl größer wird, als 32767.

    Egal. Das habe ich mit einer etwas anderen.. vielleicht etwas dick aufgetragenen Methode gelößt!

    srand (timeGetTime());
    	int Zufallszahl1 = (rand()%32767)+1;
    	srand (timeGetTime());
    	int Zufallszahl2 = (rand()%32767)+1 * Zufallszahl1;
    	srand (timeGetTime());
    	int Zufallszahl3 = (rand()%32767)+1 * Zufallszahl2;
    	srand (timeGetTime());
    	int Zufallszahl4 = (rand()%32767)+1 * Zufallszahl3;
    	srand (timeGetTime());
    	int Zufallszahl5 = (rand()%32767)+1 * Zufallszahl4;
    	srand (timeGetTime());
    	int Zufallszahl6 = (rand()%32767)+1 * Zufallszahl5;
    	srand (timeGetTime());
    	int Zufallszahl7 = (rand()%32767)+1 * Zufallszahl6;
    	srand (timeGetTime());
    	int Zufallszahl8 = (rand()%32767)+1 * Zufallszahl7;
    	srand (timeGetTime());
    	int Zufallszahl9 = (rand()%32767)+1 * Zufallszahl8;
    	srand (timeGetTime());
    	int Zufallszahl10 = (rand()%32767)+1 * Zufallszahl9;
    	srand (timeGetTime());
    	int Zufallszahl = (rand()%32767)+1 * Zufallszahl10;
    	srand (timeGetTime());
    	int Name = (rand()%32767)+1 * Zufallszahl;
    

    Aber wie geht das jetzt mit dem "Name.txt", dass auch wirklich eine "*Zufallszahl*.txt" gebildet wird?



  • Du machst einen stringstream, schiebst da den int mit der Zufallszahl rein (spart das eigene parsen), dann extrahierst du den string und wandelst den schließlich in der initialisierung von deinem fstream in einen c_string um.

    #include <sstream>
    
    ostringstream temp;
    temp << Zufallszahl;
    
    ofstream file( temp.str().c_str() );
    file << "blawurst" << endl;
    file.close();
    


  • oh, sorry... damit ne .txt wird musst du natürlich noch ".txt" hinter der Zahl herschieben, also

    temp << Zufallszahl << ".txt";



  • Jo vielen Dank! Aber wie nenne ich jetzt die Datei noch zu .txt um? Weil einfach nur ne Zufallszahl geht net ;P



  • Jetzt gehts! Vielen Dank! 😃


Anmelden zum Antworten