19.01.2017, 07:04
(
Последний раз редактировалось Lordzy; 19.01.2017 в 12:20.
)
ShuffleArray - Function shuffles the items in the given array.
Parameters:
array[] - The array to be shuffled.
start = 0 - The starting point of the array index to start shuffling. (optional)
end = sizeof(array) - The ending point of the array index to be shuffled. (optional)
Example:
Output:
I believe this function can be even more randomized to make it better, I'll work on it when I got some free time and mostly some interest.
Parameters:
array[] - The array to be shuffled.
start = 0 - The starting point of the array index to start shuffling. (optional)
end = sizeof(array) - The ending point of the array index to be shuffled. (optional)
pawn Код:
stock ShuffleArray(array[], start = 0, end = sizeof(array)) {
new
rand_Value,
i = start,
arr_Index = start,
temp_Var
;
while(i < end) {
rand_Value = random(end - start) + start;
temp_Var = array[arr_Index];
array[arr_Index] = array[rand_Value];
array[rand_Value] = temp_Var;
arr_Index = rand_Value;
i++;
}
return 1;
}
pawn Код:
public OnFilterScriptInit() {
new
my_Array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
tempStr[25],
shortStr[5]
;
print("Before shuffling, my_Array = ");
for(new i = 0; i< 10; i++) {
valstr(shortStr, my_Array[i], false);
strcat(tempStr, shortStr, sizeof(tempStr));
strcat(tempStr, " ", sizeof(tempStr));
}
print(tempStr);
tempStr[0] = '\0';
print("After shuffling from 0 -> 10 (start = 0 | end = 10)");
ShuffleArray(my_Array); //default arguments start = 0, end = sizeof(array)
for(new i = 0; i< 10; i++) {
valstr(shortStr, my_Array[i], false);
strcat(tempStr, shortStr, sizeof(tempStr));
strcat(tempStr, " ", sizeof(tempStr));
}
print(tempStr);
tempStr[0] = '\0';
print("Default array again : ");
my_Array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for(new i = 0; i< 10; i++) {
valstr(shortStr, my_Array[i], false);
strcat(tempStr, shortStr, sizeof(tempStr));
strcat(tempStr, " ", sizeof(tempStr));
}
print(tempStr);
tempStr[0] = '\0';
print("After shuffling from 5 -> 10 (start = 5 | end = 10)");
ShuffleArray(my_Array, 5, sizeof(my_Array)); //sizeof(my_Array) here is 10.
for(new i = 0; i< 10; i++) {
valstr(shortStr, my_Array[i], false);
strcat(tempStr, shortStr, sizeof(tempStr));
strcat(tempStr, " ", sizeof(tempStr));
}
print(tempStr);
return 1;
}
Код:
Before shuffling, my_Array = 1 2 3 4 5 6 7 8 9 10 After shuffling from 0 -> 10 (start = 0 | end = 10) 2 10 3 1 5 8 6 9 7 4 Default array again : 1 2 3 4 5 6 7 8 9 10 After shuffling from 5 -> 10 (start = 5 | end = 10) 1 2 3 4 5 7 6 10 8 9