rand() giving weird results in Code::Blocks/MinGW

Hi.

First: I'm quite a complete newbie when it comes to C/C++. but not programming in general. I've never written any very large programs, but I've dabbled around in a dozen or so languages. I can usually figure out the logic, but in any new language, I need to learn the syntax :)

So, I'm currently using the newest (as of 2013-03-19) Code::Blocks, with built-in GNU compiler (I believe that was MinGW).

I have a bit of a problem with random numbers. I just need a very simple semi-random number for a die roller test.

But this.
1
2
3
4
5
6
int main() {
    int diceroll = (rand() % 6) + 1;
    std::cout << diceroll;

    return 0;
}


This is making me lose my hair. I believe that is supposed to give me a semi-random spread of numbers between 1 and 6, right? Well, at least on my computer, with my environment, it doesn't. I regularly get result ranging from 0 to 7. If I omit the +1, I get results ranging from -1 to 6. I don't get it. I really don't.

What am I doing wrong? Or have I configured my environment somehow wrong? Or does Code::Blocks/MinGW have an issue with rand()?

I'm using Win7 x64, if that matters.
You're not seeding the RNG. ;)
oh ----, of course I forgot to write that. Sorry, still early for me here. Of course I'm seeding it, I just typed that out without pasting the whole code here (because I'm not at that computer that has the software...)

I'm using

srand(time(NULL));

for seeding. And of course I'm #including needed things to get it to work, before someone manages to point that out :D
Are you by any chance calling srand every time you make a call to rand? If so you should only be calling srand once.

1
2
3
4
5
6
7
8
9
10
11
12
13
// Good
srand(time(NULL));
for (int i = 0; i < 10; i++)
{
    cout << rand() % 10 << endl;
}

// Bad
for (int i = 0; i < 10; i++)
{
    srand(time(NULL));
    cout << rand() % 10 << endl;
}
Last edited on
You are probably doing something wrong. It's hard to guess what it is when we can't see the code.
Well, currently I am calling it "every time", literally, but it shouldn't matter because the program quits afterwards :) (ie, it is really only making one call to both srand and rand, then outputting the result and then exiting.)

also, calling srand everytime should affect the range of rand()? It should only make it less random and more, well, stupid :) But rand() % 6 + 1 should always output in range of 1-6, regardless of seed, if I'm not mistaken?

@Peter87: While that may be, it seems highly unlikely that anything else the program does manages to affect that range (since nothing else uses rand() or that variable.

I'll post a more complete code when possible and even test it on another computer if I can. (currently I'm having WAY too much trouble with CodeBlocks setup not installing on my other computer....)
Oh, sorry, I misread the problem. :B You're right, rand() % 6 + 1 should indeed be giving you a range of 1 to 6. I can't think of what would mess with the range like that. Which makes me think it's not actually rand that's the problem. Anything mod 6 + 1 should give you a range from 1 to 6.
Last edited on
Topic archived. No new replies allowed.