This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

gcc newbie: rand()


/*  hi there!
*
*  can anybody tell my why the following code compiled with:............

*  gcc -g -D_GNU_SOURCE filename.c -o outputfile
*
*  ....generates all sort of strange numbers but when compiled under
borland gives me the desired *   random numbers between 1 and 6  ??? how
do i do it to get the numbers between 1 and 6.
*  this example is from a book about C so it can't be that wrong, can
it?
*/

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

#define SEED 12345

main()

{

    float x;
    int n;

        srand(SEED);

            x = rand() /32768.0;
            n = 1 + (int) (6 * x);

      printf("x = %d", n);
}

/*Thank's
                    Joachim!
*/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]