problem with switch statement
- creekmonkey
- Posts: 116
- Joined: Tue Oct 23, 2007 2:55 pm
problem with switch statement
I have a switch statement to randomly switch between 4 script points. It only will switch between the first 3. Is this correct or should it be switch(random(0,4)? or switch (random(1,5)?
switch(random(1,4)) // chose between 4 script points
{
case 1
{
TeleportToPoint("b1",0,0,0);
NewPoint("b1");
RotateToAlign("idle",500,false,"");
}
case 2
{
TeleportToPoint("b2",0,0,0);
NewPoint("b2");
RotateToAlign("idle",500,false,"");
}
case 3
{
TeleportToPoint("b3",0,0,0);
NewPoint("b3");
RotateToAlign("idle",500,false,"");
}
case 4
{
TeleportToPoint("b4",0,0,0);
NewPoint("b4");
RotateToAlign("idle",500,false,"");
}
}
Not sure which should be correct bu changing it to switch(random(1,5) works.
switch(random(1,4)) // chose between 4 script points
{
case 1
{
TeleportToPoint("b1",0,0,0);
NewPoint("b1");
RotateToAlign("idle",500,false,"");
}
case 2
{
TeleportToPoint("b2",0,0,0);
NewPoint("b2");
RotateToAlign("idle",500,false,"");
}
case 3
{
TeleportToPoint("b3",0,0,0);
NewPoint("b3");
RotateToAlign("idle",500,false,"");
}
case 4
{
TeleportToPoint("b4",0,0,0);
NewPoint("b4");
RotateToAlign("idle",500,false,"");
}
}
Not sure which should be correct bu changing it to switch(random(1,5) works.
Re: problem with switch statement
LOL. You are the FIRST one to actually notice the bug in the random command... (I noticed it before, and changed it in the source, but interestingly it's been there for YEARS and no one ever noticed it besides me lol). Yes, it is somehow quirked so random(1,x) does only return 1-(x-1), so random(1,3) returns 1 or 2. I think noone noticed this before because normally scripts use random(1,100)>x, where the ratio of 100/99 is not that big, but 3/2 is bigger.
I noticed it in the exact same situation you have (with the switch statement): Implementing random soundtracks. The last one was never played.
It's a bug. As a workaround use random(1,5).
I noticed it in the exact same situation you have (with the switch statement): Implementing random soundtracks. The last one was never played.
It's a bug. As a workaround use random(1,5).
Everyone can see the difficult, but only the wise can see the simple.
-----
-----
Re: problem with switch statement
The random number formula should be:
not
Replace utilities.cpp line 621 with the formula I specified above.
Code: Select all
srand(time(NULL));
rand() % ((max +1) - min)) + min
Code: Select all
Range = High - Low;
return ((float)(((rand() % 1000) + 1))) / 1000.0f * Range + Low;
Many Bothans died to bring you this signature....
- QuestOfDreams
- Site Admin
- Posts: 1520
- Joined: Sun Jul 03, 2005 11:12 pm
- Location: Austria
- Contact:
Re: problem with switch statement
There's no bug in that formula. BUT: the current one is for floats but is also used for integers by simply casting the result to int which of course truncates the number instead of rounding it. That's why it is rather improbable that the function returns the max value.
(e.g. random(1, 4): 1.0 to 1.999 -> 1; 2.0 to 2.999 -> 2; 3.0 to 3.999 -> 3; 4.0 -> 4 )
paradoxnj, your version only works if min and max are integers (no % operation for floats)
(e.g. random(1, 4): 1.0 to 1.999 -> 1; 2.0 to 2.999 -> 2; 3.0 to 3.999 -> 3; 4.0 -> 4 )
paradoxnj, your version only works if min and max are integers (no % operation for floats)
Re: problem with switch statement
ah. So no bug, but very unprobable. Wouldn't it be better to just round the values in this case then? It's very easy to do so, too.
Would random(1,4.999) be a workaround currently? (as random(1,5) can sometimes return 5, which could be unfavorable)
Code: Select all
int round(float input)
{
int lastn=(int)(input*10);
lastn%=10;
if(lastn>4)
return (int)(input+1);
return (int)input;
}
Everyone can see the difficult, but only the wise can see the simple.
-----
-----
- QuestOfDreams
- Site Admin
- Posts: 1520
- Joined: Sun Jul 03, 2005 11:12 pm
- Location: Austria
- Contact:
Re: problem with switch statement
Just added a new function for random integers ...
Re: problem with switch statement
even better
Everyone can see the difficult, but only the wise can see the simple.
-----
-----
Re: problem with switch statement
Use the fmod() function to replace the % operator for floats. Formula works if you use the fmod() function. Also, you should be using frand() to generate random floats, not rand().paradoxnj, your version only works if min and max are integers (no % operation for floats)
Many Bothans died to bring you this signature....
- QuestOfDreams
- Site Admin
- Posts: 1520
- Joined: Sun Jul 03, 2005 11:12 pm
- Location: Austria
- Contact:
Re: problem with switch statement
Why should I use fmod? The current version for floats works fine. Also I can't find a frand() function in the standard c/c++ libraries.
Re: problem with switch statement
Because you are doing float to integer conversions using casting which is extremely slow. Converting an integer to a float does not produce correct floats either.Why should I use fmod?
It's in the STL and is also part of Boost.Also I can't find a frand() function in the standard c/c++ libraries
Many Bothans died to bring you this signature....
Re: problem with switch statement
The rand_s() function produces a double value and it's declared in stdlib.h just like the rand() function.
- QuestOfDreams
- Site Admin
- Posts: 1520
- Joined: Sun Jul 03, 2005 11:12 pm
- Location: Austria
- Contact:
Re: problem with switch statement
That's not correct. The rand_s function is just a version of rand with security enhancements.The rand_s() function produces a double value and it's declared in stdlib.h just like the rand() function.
Re: problem with switch statement
Sorry, you are correct. I took a quick look at THIS example without reading or looking too closely at the code and thought rand_s() was returning a double value. In my defense I have been up all night and probably need some sleep.