Function Rand(0,1)

edward77
Sergeant - Panzer IIC Posts: 192
Joined: Wed Nov 10, 2010 9:11 pm

Function Rand(0,1)

Richard, I have a strong suspicion that Rand(0,1) does not work but maybe I am doing something wrong elsewhere. I'm creating an alternative Yamazaki scenario in which there are two possible attack plans for the AI Akechi where Rand(0,1) decides which one is selected either defensive or all out attack. I firstly used 1 as the all out attack and for five successive runs it was never selected. I then swapped the options with 0 being the all out attack option. Again I had five successive runs all of which were all out attack. In total that is 10 successive selections of 0. The probability of this is (1/2)^10. Here is the code where the offender is b which is always 0. Similarly a is also always 0:-

// Scenario-specific AI code goes here - you can look at some of the historical scenarios for examples.

int turn;
int team:
int id:
int i;
int j;
int v;
int a;
int b;
int visibility;
turn = GetTurn();

if ((turn == 0) || (turn == 1))
{
visibility = 25;
}

if ((turn == 2) || (turn == 3))
{
visibility = 45;
}

if ((turn == 4) || (turn == 5))
{
visibility = 75;
}

if (turn > 5)
{
visibility = 110;
}

if (turn > 7)
{
visibility = 180;
}

for (j = 0; j < 2; j++)
{
for (i = 0; i < GetUnitCount(j); i++)
{
id = GetUnitID(j,i);
if (id != -1)
{
v = visibility;
// Increase visibility for units on heights
// if (GetTileHeight(GetUnitX(id), GetUnitY(id)) >= 50)
// {
// v += 10;
// }
SetAttrib(id, "LOS", v);
}
}
}

if (turn == 0)
{
AddVizCamCenter (56, 56); // Shoryuji Castle
AddVizCamCenter (38, 46); // Enmyojigawa
AddVizCamCenter (37, 28); // Tennozan
AddVizCamCenter (22, 33); // Yamazaki
AddVizCamCenter (31, 21); // Forest 1
AddVizCamCenter (45, 20); // Forest 2
AddVizCamCenter (59, 19); // Forest 3
a = Rand(0,1);
b = Rand(0,1);
}

if (turn == 1)
{
MoveTeamCoord( 1, 2, 27, 49, 24);
MoveTeamCoord( 1, 3, 29, 43, 24);
MoveTeamCoord( 1, 4, 31, 41, 24);
MoveTeamCoord( 1, 5, 34, 39, 24);
MoveTeamCoord( 1, 6, 37, 38, 24);
MoveTeamCoord( 1, 7, 32, 44, 24);
}

if ((turn == 1) & (a == 1))
{
MoveTeamCoord( 1, 0, -1, -1, 64);
}

if ((turn == 1) & (b == 0))
{
MoveTeamCoord( 1, 2, -1, -1, 16);
MoveTeamCoord( 1, 3, -1, -1, 16);
MoveTeamCoord( 1, 4, -1, -1, 16);
MoveTeamCoord( 1, 5, -1, -1, 16);
MoveTeamCoord( 1, 6, -1, -1, 16);
MoveTeamCoord( 1, 7, -1, -1, 16);
}

if ((turn == 3) & (b == 0))
{
AddVizCamCenter (38, 46); // All out attack
}

if (turn == 9)
{
AddVizCamCenter (40, 61); // Hosokawa Reinforce
MoveTeamCoord( 1, 1, 45, 40, 24);
}

rbodleyscott
Field of Glory 2 Posts: 22237
Joined: Sun Dec 04, 2005 6:25 pm

Re: Function Rand(0,1)

This is an error in your code logic, not a problem with Rand(0,1).

a and b are local variables, so they are not stored anywhere when the program leaves the function.

You set them using Rand(0,1) on turn 0, and then when the function is called on turn 1 they have been created anew with the default value of 0.

You need to use a variable that won't get reset.

in ARCHON (FOG2) you could use a global variable, but in STUB (SJ and P&S) I suggest using a UniversalVariable:

e.g.
if (turn == 0)
{
...
...
SetUniversalVar("RandomPlanA", Rand(0,1));
SetUniversalVar("RandomPlanB", Rand(0,1));
}

if ((turn == 1) & (GetUniversalVar("RandomPlanA") == 1))
{
MoveTeamCoord( 1, 0, -1, -1, 64);
}
Richard Bodley Scott edward77
Sergeant - Panzer IIC Posts: 192
Joined: Wed Nov 10, 2010 9:11 pm

Re: Function Rand(0,1)

Oh right. Thank you for that.