Random Number function problem
I have a function like such
Code:
#include <cstdlib>
inline int randInt(int limit)
{
return limit <= 1 ? 0 : (rand() % limit);
}
And i use it as followd
Code:
if(randInt(2) == 1)
{
column = ((randInt(2) == 1) ? (lastTemp.c + randInt(4)) : (lastTemp.c - randInt(4)));
row = lastTemp.r;
}
else
{
column = lastTemp.c;
row = ((randInt(2) == 1) ? (lastTemp.r + randInt(4)) : (lastTemp.r - randInt(4)));
}
And my lastTemp.c and latTemp.r are both = 0;
I get the same sequence every time. which is
r - c
0 - 0
0 - 3
1 - 0
2 - 1
0 - 2
3 - 0
how come i dont get 2 - 0 ?
Re: Random Number function problem
Try stepping through in the debugger.
Until you get it working, you may want to try to simplify your code so you don't have so much going on one one line. You may have more lines of code, but sometimes that's a good tradeoff to make it more readable and easier to debug.
Re: Random Number function problem
Your code looks fine to me.
I suspect that you forgot to call srand( time( NULL ) ); at the beginning of your program.
Hope this helps.
Re: Random Number function problem