[英]% doesn't work after rand() c++
I have to use fmod function instead of % on this code:我必须在此代码中使用 fmod 函数而不是 % :
int prob = rand();// % 4;
prob = fmod(prob,4);
if (prob == 0) {
shoot = true;
}
else shoot = false;
if I use %, prob variable it doesn't get any value on debuger.如果我使用 %, prob 变量,它在调试器上不会得到任何值。
Second problem is it never gets inside if, I put breakpoint on if, another on else, and other on shoot = true;第二个问题是它永远不会进入 if,我把断点放在 if 上,另一个放在 else 上,其他放在 shot = true 上; the debuger says: The breakpoint will not currently be hit.
调试器说:当前不会命中断点。 No executable code of the debugger's target code type is associated.
没有关联调试器目标代码类型的可执行代码。
On other computer the second problem doesn't exist.在其他计算机上,第二个问题不存在。
Full function:全功能:
void cVoladorEstatico::Logic(int *map)
{
double t1 = glutGet(GLUT_ELAPSED_TIME);
if (t1 - moveDelaySteering > 20 * 20) {
random_variable = rand();
moveDelaySteering = t1;
}
if (t1 - moveDelay > 20) {
int aux = y;
y += random_variable%speed - 3;
//Whats next tile?
if ((y % TILE_SIZE) <= 1)
{
/*y += speedY;
x += speedX;*/
//si choca con tile, se autodestruye muy fuerte
if (CollidesMapWall(map, false)) y = aux; //delete this;
}
//Advance, no problem
else
{
/*y += speedY;
x += speedX;*/
//TODO: si choca, hace magia
}
moveDelay = t1;
}
//should i shoot
//if (t1 - lastShootDec > shootChance) {
int prob = rand();// % 4;
prob = fmod(prob,4);
if (prob == 0) {
shoot = true;
}
else shoot = false;
//}
}
The original code should work just fine.原始代码应该可以正常工作。 Chances are the debugger isn't telling you the truth;
很有可能调试器没有告诉你真相。 any good optimizer will skip storing the value into
prob
, since it's only used in the condition of the if
statement.任何好的优化器都会跳过将值存储到
prob
,因为它仅用于if
语句的条件中。 So write out the value after computing prob
to see what's going on.所以在计算
prob
后写出这个值,看看发生了什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.