12.10.2018, 09:36
Very useful include. I can use it as a stack for a non-recursive backtracking.
For anyone interested, I needed a random shuffle for one of my vectors. It is based on Fisher-Yates's algorithm (modern version):
Although it is still random shuffle, I prefer using MerRandom plugin.
For anyone interested, I needed a random shuffle for one of my vectors. It is based on Fisher-Yates's algorithm (modern version):
pawn Code:
VECTOR_random_shuffle(Vector: vector, first, last)
{
for (new i = (last - first) - 1; i > 0; --i)
{
VECTOR_swap(vector, i, random(i + 1));
}
}
VECTOR_swap(Vector: vector, a, b)
{
new a_value = VECTOR_get_val(vector, a);
VECTOR_set_val(vector, a, VECTOR_get_val(vector, b));
VECTOR_set_val(vector, b, a_value);
}
pawn Code:
// Example:
public OnGameModeInit()
{
new Vector: vector;
for (new i = 1; i <= 10; i++)
{
VECTOR_push_back_val(vector, i);
}
VECTOR_random_shuffle(vector, 0, VECTOR_size(vector));
VECTOR_foreach(v : vector)
{
printf("%d", MEM_get_val(v));
}
return 1;
}