Page 1 of 1

problem with switch statement

Posted: Tue Apr 08, 2008 10:56 pm
by creekmonkey
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.

Re: problem with switch statement

Posted: Wed Apr 09, 2008 7:12 pm
by Jay
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).

Re: problem with switch statement

Posted: Thu Apr 10, 2008 12:01 am
by paradoxnj
The random number formula should be:

Code: Select all

srand(time(NULL));
rand() % ((max +1) - min)) + min
not

Code: Select all

Range = High - Low;
return ((float)(((rand() % 1000) + 1))) / 1000.0f * Range + Low;
Replace utilities.cpp line 621 with the formula I specified above.

Re: problem with switch statement

Posted: Sat Apr 12, 2008 12:06 pm
by QuestOfDreams
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)

Re: problem with switch statement

Posted: Sat Apr 12, 2008 2:34 pm
by Jay
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.

Code: Select all

int round(float input) 
{
   int lastn=(int)(input*10);
   lastn%=10;
   if(lastn>4)
      return (int)(input+1);
   return (int)input;
}
Would random(1,4.999) be a workaround currently? (as random(1,5) can sometimes return 5, which could be unfavorable)

Re: problem with switch statement

Posted: Sat Apr 12, 2008 3:54 pm
by QuestOfDreams
Just added a new function for random integers ...

Re: problem with switch statement

Posted: Sat Apr 12, 2008 7:45 pm
by Jay
even better

Re: problem with switch statement

Posted: Sun Apr 13, 2008 1:23 am
by paradoxnj
paradoxnj, your version only works if min and max are integers (no % operation for floats)
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().

Re: problem with switch statement

Posted: Sun Apr 13, 2008 2:10 am
by QuestOfDreams
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

Posted: Sun Apr 13, 2008 2:39 am
by paradoxnj
Why should I use fmod?
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.
Also I can't find a frand() function in the standard c/c++ libraries
It's in the STL and is also part of Boost.

Re: problem with switch statement

Posted: Sun Apr 13, 2008 12:23 pm
by Allanon
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

Posted: Sun Apr 13, 2008 1:41 pm
by QuestOfDreams
The rand_s() function produces a double value and it's declared in stdlib.h just like the rand() function.
That's not correct. The rand_s function is just a version of rand with security enhancements.

Re: problem with switch statement

Posted: Sun Apr 13, 2008 2:07 pm
by Allanon
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.