【发布时间】:2017-06-24 22:58:03
【问题描述】:
我正在尝试在 GML 中创建一个洗牌数组函数。这是我尝试过的,argument0 是要洗牌的数组,argument1 是这个数组的大小:
///Shuffling array function
//argument0: the array to shuffle
//argument1: the size of the array
var i;
var j;
show_debug_message("----------");
show_debug_message("Original array: ");
show_debug_message(argument0);
show_debug_message("Size: ");
show_debug_message(argument1);
for (i = 0; i < argument1; i++)
{
j = irandom_range(i, argument1 - 1);
if (i != j)
{
k = argument0[i];
argument0[i] = argument0[j];
argument0[j] = k;
}
}
show_debug_message("Result array: ");
show_debug_message(argument0);
show_debug_message("----------");
return argument0;
当我执行这个函数时,我总是得到相同的结果:
----------
Original array:
{ { 1,2,3,4,5 }, }
Size:
5
Result array:
{ { 5,3,1,4,2 }, }
----------
【问题讨论】:
-
您是否尝试过使用更大的数组来确认这一点?它有有限数量的元素可以随机播放,但是你有很小的机会随机地一次又一次地获得相同的元素。或者你可以多次调用 shuffle。
标签: game-maker gml game-maker-language game-maker-studio-2 game-maker-studio-1.4