How to find average

im looking for a way to detect and average of a set of numbers for example: say we have 12,95 54,32 83,17 i want it to pick the pair most near each other in this case 54,32 need holp

You iterate over the set of numbers in a for or while loop, within that loop you add them up into a sum and assign that sum to a variable, finally after the loop or outside the loop you divide that sum variable by the numbers list length and assign that result to a second variable called average if you want, the result should be the average, if you want a whole number of it you can round that result while you assign it to a variable with Round To Integer function. If you want i can provide a code snippet for demonstration.

1 Like

that’s just the average i want it to pick the average NUMBER PAIR not average all the numbers like if i had he list i want it to pick the number in the middle in terms of value

Ah you want to pick the value at the average position in the set and its nearest neighbour close to that position in the list. Then you can do this as follows:

variables
{
	global:
		0: SumIndex
		1: AverageIndex
		2: Pair
		3: List
}

rule("Rule 1")
{
	event
	{
		Ongoing - Global;
	}

	actions
	{
		Global.List = Array(12, 95, 54, 32, 83, 17);
		For Global Variable(I, 0, Count Of(Global.List), 1);
			Global.SumIndex += Global.I;
		End;
		Global.AverageIndex = Round To Integer(Global.SumIndex / Count Of(Global.List), Up);
		Global.Pair = Array(Global.List[Global.AverageIndex - 1], Global.List[Global.AverageIndex]);
	}
}
1 Like

thats what i need but is there a way to do that for pairs but still in an array like an array with (12,54, 58,32);

I am gettin educated !

Sure just set up the Array you want you can also generate Arrays randomly for testing, be aware the Array size limit is 1000 so ranging from index 0-999. For what do you actually need such operations btw? The essential part are these:

For Global Variable(I, 0, Count Of(Global.List), 1);
			Global.SumIndex += Global.I;
		End;
		Global.AverageIndex = Round To Integer(Global.SumIndex / Count Of(Global.List), Up);
		Global.Pair = Array(Global.List[Global.AverageIndex - 1], Global.List[Global.AverageIndex]);

The list you iterate over can be anything, even dynamically modified at runtime for any game mode you need such thing for.

1 Like

i’m using them for an AI path finding. basically if the AI dosn’t see the player and reaches distance to a wall while trying to go to the player it will fire 8 raycasts in each direction and id would go in the direction of the ray cast farthest away but closest to player to maximize the chance of going around the wall

I tried exactly this method a couple of years ago. It kinda works, but it is very unreliable and I needed a lot of tweaking to overcome special situations (and there are a lot of things that can go the wrong way).

Instead I developed a graph based pathfinding algorithm. The idea is to give the bots a fixed set of walkable paths and the bot chooses the best path to reach the current target.
Feel free to use my tool. I will update it with some qol changes once I got the time to do so:

If you really want to program it yourself (because it is indeed a lot of fun to do - at least it was for me :slight_smile: ) you could research about graphs (computer science) and the Dijkstra algorithm to get you started.