4

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.

  • Possible duplicate of [How to generate different random numbers in a loop in C++?](http://stackoverflow.com/questions/4926622/how-to-generate-different-random-numbers-in-a-loop-in-c) – Thomas Ayoub Oct 05 '15 at 08:16
  • 1
    Please provide a short but complete example which demonstrates the problem -- one that we can compile and run to reproduce the problem. – David Schwartz Oct 05 '15 at 08:16
  • 2
    @Thomas The OP didn't put the `srand()` calls inside the loop. So it is not a duplicate. – fjardon Oct 05 '15 at 08:16
  • 1
    can you clarify if it is `rand()` or `rand()%3` that give a 0 result? – Sigve Kolbeinson Oct 05 '15 at 08:17
  • `rand()` is clock based, and in this kind of very fast loop, your clock doesn't change, that's why you always get the same numbers – Thomas Ayoub Oct 05 '15 at 08:17
  • 8
    @Thomas That's simply not true, `rand()` is not clock based. – David Schwartz Oct 05 '15 at 08:17
  • Possible duplicate of [rand() generating the same number – even with srand(time(NULL)) in my main!](http://stackoverflow.com/questions/3032726/rand-generating-the-same-number-even-with-srandtimenull-in-my-main) – Pooja Nilangekar Oct 05 '15 at 08:19
  • @PoojaNilangekar This is not the same problem. Your link is about generating the same sequence of numbers but the sequence contains different values. – fjardon Oct 05 '15 at 08:25
  • Thank you everyone for your replies. In response to Sigve Kolbeinson's question, I have confirmed that rand() returns a result of 0 ( not just (rand % 3)). –  Oct 05 '15 at 08:33
  • 2
    How did you confirm? This worked on my compile testing `g_aBld`. In optimised build, this may not be seen as it populates array – mksteve Oct 05 '15 at 09:07
  • Check all you include files for a potential redefinition of `rand`. Add `#undef rand` before the call to `srand`. – chqrlie Oct 05 '15 at 09:08
  • [Why does rand() % 7 always return 0?](http://stackoverflow.com/q/7866754/995714), although I don't think that VS uses such terrible random generator – phuclv Oct 05 '15 at 10:20
  • OP errantly determined `rand()`--> 0. `g_aBld[cntBld].type` never used. Code has no output. Debugging can differ from the the non-debugged code real-time flow. OP should add `printf("%d\n", g_aBld[cntBld].type);` to generate observable output to an otherwise black box. – chux - Reinstate Monica Oct 08 '15 at 03:49

2 Answers2

1
     g_aBld[cntBld].type = (rand() % 3);

Don't use mod to reduce the range of rand because this can interoperate badly with the way your random number generator initializes itself. Try, for example:

     g_aBld[cntBld].type = rand() / (RAND_MAX / 3);
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Thank you for your reply! Unfortunately, the code you provided did not seem to work within my program. I have confirmed that the rand() function itself is behaving strangely, and not only (rand() % 3). Any ideas on why the method itself would not be behaving correctly? –  Oct 05 '15 at 08:37
  • Give us enough code so that we can compile and run it. What return values are you seeing from `rand()`? – David Schwartz Oct 05 '15 at 08:40
  • Understood. I have edited my original post to include the main function with a call to the InitBuilding() function. All values from rand() are zero, regardless of the range span. –  Oct 05 '15 at 08:51
  • That provides no way to tell what value was returned from `rand`. So we still don't know how you've determined that it's returning zero. Did you try changing `g_aBld[cntBld].type = (rand() % 3);` to `printf("rand=%d\n", rand());`? – David Schwartz Oct 05 '15 at 08:53
1

Project compiled with the addition of

int main( int argc, char ** argv )
{
    InitBuilding();
}

With this added the code worked producing types of 0, 1, 2

mksteve
  • 12,614
  • 3
  • 28
  • 50
  • The original post wasn't an MCVE. It didn't call the function to set the data. With a call from main to the function, the values returned by rand vary each time and correctly set the value type in the array. – mksteve Oct 05 '15 at 08:42
  • Do you mean you can run a program without a `main` function in Visual Studio? – David Ranieri Oct 05 '15 at 08:57