Zufallszahlen



  • Ich kann aber mit count und cin nix anfangen.



  • Ich habe auch nichts von cout oder cin gesagt 😉

    Dein printf() ist falsch. So versuchst du ein double als integer auszugeben. Das kann nicht gutgehen. Deine Aufgabe ist es jetzt herauszufinden wie man mit printf() ein double ausgibt. Das steht in einem Buch, oder in der Manpage von man: printf.



  • Ok dann schreib ich halt %lf für double oder %i für int. das löst aber nicht das Prob.



  • oppi25 schrieb:

    Ok dann schreib ich halt %lf für double oder %i für int. das löst aber nicht das Prob.

    Dann ist dein Problem nicht klar dargestellt, denn das Programm gibt mit der entsprechenden Änderung an printf() eine (Pseudo) Zufallszahl aus.
    Ich bemühe mal die Kristallkugel: Ist dein Problem, dass die Zahl bei jedem Aufruf dieselbe ist? Dann solltest du die Manpage zu srand() durchlesen.



  • bei int zeigt er mir immer die null an und bei double 56,36.



  • oppi25 schrieb:

    [...] zum Void main (void) frag mal unsere Dozentin!!! 👎

    zum

    void main( void ) { }
    

    frag' mal den Standard 😃

    Greetz, Swordfish



  • oppi25 schrieb:

    bei int zeigt er mir immer die null an und bei double 56,36.

    Zeig mal deinen kompletten Sourcecode und beschreib' nochmal, was du zu bewerkstelligen versuchst!

    Greetz, Swordfish



  • Ich versuche einfach, dass er mir eine Zufallszahl rausgibt zwischen 0 und 100.

    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>

    void main (void)

    {
    int x;
    x=rand();
    x =(int)rand()/(RAND_MAX)*100;
    printf("%i\n",x);
    }



  • oppi25 schrieb:

    Ich versuche einfach, dass er mir eine Zufallszahl rausgibt zwischen 0 und 100.

    dein "zwischen" interpretier ich einfach mal als 0 < z < 100

    oppi25 schrieb:

    #include <stdio.h>
    #include <math.h>                           // wofür??
    #include <stdlib.h>
    #include <time.h>                           // wofür??
    
    void main (void)                            // der Standard sagt immer noch
    {                                           // int main( )
        int x;
        x = rand();                             // wofür?
        x = (int) rand( ) / ( RAND_MAX ) * 100; // wie du auf diese Formel kommst
                                                // ist mir schleierhaft...
        printf( "%i\n" , x );
    }
    

    Ok, da hast du:

    C++:

    // Gibt eine Pseudozufallszahl z | 0 < z < 100 aus
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main( )
    {	
        srand( static_cast< unsigned int >( time( 0 ) ) );
        // initialisiert den Zufallsgenerator mit der Systemzeit
    
        int r = 1 + static_cast< int >( ( 99.0 * rand( ) / static_cast< float >( RAND_MAX ) ) );
    
        /* In Numerical Recipes in C: The Art of Scientific Computing
           (William  H.  Press, Brian P. Flannery, Saul A. Teukolsky,
           William T.  Vetterling;  New  York:  Cambridge  University
           Press,  1990 (1st ed, p. 207)), the following comments are
           made:
                "If you want to generate a random integer between 1
                 and 10, you should always do it by
    
                 j = 1 + (int)( 10.0 * rand() / ( RAND_MAX + 1.0 ) );
    
                 and never by anything resembling
    
                 j = 1 + ( (int)( 1000000.0 * rand() ) % 10 );
    
                 (which uses lower-order bits)."
        */
    
        cout << Zufallszahl: << r << endl;
    }
    

    und nochmal, extra für dich lieber oppi25, in C:

    // Gibt eine Pseudozufallszahl z | 0 < z < 100 aus
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    int main( )
    {	
        srand( ( unsigned int ) time( 0 ) );
    
        int r = 1 + ( int )( ( 99.0 * rand( ) / ( ( float ) ( RAND_MAX ) ) ) );
    
        printf( "Zufallszahl: %i\n", r );
    }
    

    Ich hoff', jetzt sind alle Klarheiten beseitigt.

    Greetz, Swordfish



  • Was (in einfachen Worten) passiert in dieser Zeile:

    static_cast< unsigned int >( time( 0 ) )

    ?

    Danke.



  • Es konvertiert den Rückgabewert von time(0) nach unsigned int, ungeachtet welchen Typs es vorher war.

    Aber ist ein time_t nicht sowieso ein unsigned Typ? 😉



  • bei mir (MSVC 2003 .net) ist time_t

    time.h schrieb:

    typedef __int64 time_t;       /* time value */
    

    Greetz, Swordfish



  • thx 4 clarifying (auf sarge ist time_t ein __time_t, also ein implementationsabhängiger Typ)

    Ist der Rückgabewert von time() bei Microsoft eigentlich auch Seconds since Epoch? cppreference.com schreibt nur "returns the current time" und die Epoch ist m.W. doch eigentlich POSIX, oder? Meine Linux-Manpage spricht auch nur vom POSIX-Standard IEEE 1003.1-2001 in dem Zusammenhang.

    (Sorry für's Thread kapern, passt grad so gut 😉 )



  • MSDN schrieb:

    The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC), according to the system clock.

    Greetz, Swordfish

    PS: Threadthema hat sich IMHO sowieso erledigt. Kapere ruhig 😉


Anmelden zum Antworten