dimanche 22 octobre 2017

Strange behaviour of rand() in Xcode

While using Apple Xcode, the C program I include below will always return a 5 for y but z will return a random number, even though they have the same expression. Can you identify a reason for this behaviour?

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

int main()
{
    int x,y,z;
    srand((unsigned int)time(NULL));
    x = ((unsigned int)time(NULL));
    y=((double)rand()/(double)(RAND_MAX)*10);
    z=((double)rand()/(double)(RAND_MAX)*10);
    printf("Time %d\n", x);
    printf("\n");
    printf("Number y %d\n", y);
    printf("\n");
    printf("Number z %d\n", z);
    printf("\n");
    return 0;
}




Aucun commentaire:

Enregistrer un commentaire