lundi 5 octobre 2015

rand() keeps returning 0

I am using Visual Studio 2010 and programming in C. I am attempting to produce a random integer value via the rand() method. Here is the code:

/*main.cpp*/
int main (void)
{
    InitBuilding();

    return 0;
}

/*building.cpp*/
//includes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//data structure
typedef struct
{
    int type;   //building type
} BUILDING;

//global variables
BUILDING g_aBld[200];

//initialization
void InitBuilding(void)
{
    srand((unsigned)time(NULL));

    for(int cntBld = 0; cntBld < 200; cntBld++)
    {  
         g_aBld[cntBld].type = (rand() % 3);
    }
}

After debugging I've realized that 0 is continually generated for each iteration of the loop. I've used this exact code before in other programs, and it worked fine. I have no idea why it wouldn't be working now. Thanks in advance for any replies.




Aucun commentaire:

Enregistrer un commentaire