I found a really weird output generated by rand()%7 in C language. The output is always 0. Could anyone help me look into this? Thanks in advance.
So here is my code:
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#include <stdlib.h>
#define BOARD_SIZE_VERT 6
#define BOARD_SIZE_HORIZ 7
int ranT(){
int m;
srand(time(0));
m=rand()%(BOARD_SIZE_HORIZ);
return m;
}
int main(){
printf("%d",ranT());
return 0;
}
If I change BOARD_SIZE_HORIZ to BOARD_SIZE_VERT in ranT(), then the output printed on the screen will be 0, 1, 2, 3, 4, or 5, all of which are expected. However, if I use BOARD_SIZE_HORIZ, the output will always be 0.
Could anyone help me understand this?
Aucun commentaire:
Enregistrer un commentaire