problem with switch statement

Topics regarding Scripting with Reality Factory
Post Reply
User avatar
creekmonkey
Posts: 116
Joined: Tue Oct 23, 2007 2:55 pm

problem with switch statement

Post 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.
Jay
RF Dev Team
Posts: 1232
Joined: Fri Jul 08, 2005 1:56 pm
Location: Germany

Re: problem with switch statement

Post 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).
Everyone can see the difficult, but only the wise can see the simple.
-----
User avatar
paradoxnj
RF2 Dev Team
Posts: 1328
Joined: Wed Mar 01, 2006 7:37 pm
Location: Brick, NJ
Contact:

Re: problem with switch statement

Post 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.
Many Bothans died to bring you this signature....
User avatar
QuestOfDreams
Site Admin
Posts: 1520
Joined: Sun Jul 03, 2005 11:12 pm
Location: Austria
Contact:

Re: problem with switch statement

Post 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)
Jay
RF Dev Team
Posts: 1232
Joined: Fri Jul 08, 2005 1:56 pm
Location: Germany

Re: problem with switch statement

Post 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)
Everyone can see the difficult, but only the wise can see the simple.
-----
User avatar
QuestOfDreams
Site Admin
Posts: 1520
Joined: Sun Jul 03, 2005 11:12 pm
Location: Austria
Contact:

Re: problem with switch statement

Post by QuestOfDreams »

Just added a new function for random integers ...
Jay
RF Dev Team
Posts: 1232
Joined: Fri Jul 08, 2005 1:56 pm
Location: Germany

Re: problem with switch statement

Post by Jay »

even better
Everyone can see the difficult, but only the wise can see the simple.
-----
User avatar
paradoxnj
RF2 Dev Team
Posts: 1328
Joined: Wed Mar 01, 2006 7:37 pm
Location: Brick, NJ
Contact:

Re: problem with switch statement

Post 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().
Many Bothans died to bring you this signature....
User avatar
QuestOfDreams
Site Admin
Posts: 1520
Joined: Sun Jul 03, 2005 11:12 pm
Location: Austria
Contact:

Re: problem with switch statement

Post 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.
User avatar
paradoxnj
RF2 Dev Team
Posts: 1328
Joined: Wed Mar 01, 2006 7:37 pm
Location: Brick, NJ
Contact:

Re: problem with switch statement

Post 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.
Many Bothans died to bring you this signature....
Allanon
Posts: 493
Joined: Mon Aug 29, 2005 8:23 am

Re: problem with switch statement

Post by Allanon »

The rand_s() function produces a double value and it's declared in stdlib.h just like the rand() function.
User avatar
QuestOfDreams
Site Admin
Posts: 1520
Joined: Sun Jul 03, 2005 11:12 pm
Location: Austria
Contact:

Re: problem with switch statement

Post 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.
Allanon
Posts: 493
Joined: Mon Aug 29, 2005 8:23 am

Re: problem with switch statement

Post 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.
Post Reply