使用JavaScript中的单个预定义选项创建随机数组
我正在创建一个程序,该程序将提出一个问题,并为答案提供5个选项。使用JavaScript中的单个预定义选项创建随机数组,javascript,arrays,random,Javascript,Arrays,Random,我正在创建一个程序,该程序将提出一个问题,并为答案提供5个选项。 一个是预定义的,并且是正确的,其他的我希望是从答案库中随机选择的,整个数组也将被洗牌 我写了一些东西,但有些不一致。 例如,有时预定义的选项会在列表中出现两次(它似乎会跳过我的ifcheck)。 另一个原因是,有时,当我运行编辑器时,它会崩溃。 我对in循环使用,我担心崩溃是由一个永无止境的循环造成的 这是我的密码: private var numberOfComponents:int; private var maxCompon
一个是预定义的,并且是正确的,其他的我希望是从答案库中随机选择的,整个数组也将被洗牌 我写了一些东西,但有些不一致。
例如,有时预定义的选项会在列表中出现两次(它似乎会跳过我的
if
check)。
另一个原因是,有时,当我运行编辑器时,它会崩溃。我对in循环使用
,我担心崩溃是由一个永无止境的循环造成的
这是我的密码:
private var numberOfComponents:int;
private var maxComponents:int = 5;
//numberOfComponents returns the length property of my 'components' answer bank
componentsSelection = buildComponentSelectionList(0); //0 is the index of my correct answer
function buildComponentSelectionList(correctItemIndex){
var theArray:Array = new Array();
var indicesOfSelection:Array = getIndicesByIncluding(correctItemIndex);
Debug.Log(indicesOfSelection);
for (var i=0;i<indicesOfSelection.length;i++)
theArray.Push(components[indicesOfSelection[i]]);
return theArray;
}
function getIndicesByIncluding(correctItem){
var indicesArray:Array = new Array();
var numberOfChoices = maxComponents-1;
for(var i=0;i<numberOfChoices;i++){
var number = Mathf.Round(Random.value*(numberOfComponents-1));
addToRandomNumberSelection(indicesArray, number,correctItem);
}
indicesArray.Push(correctItem);
RandomizeArray(indicesArray);
return indicesArray;
}
function addToRandomNumberSelection(indicesArray:Array,number,correctItem){
if(indicesArray.length == 0){
indicesArray.Push(number);
} else {
var doesntExist = true;
for(var i=0;i<indicesArray.length;i++){
if(indicesArray[i] == correctItem)
doesntExist = false;
if (indicesArray[i] == number)
doesntExist = false;
}
if(doesntExist) {
indicesArray.Push(number);
} else {
addToRandomNumberSelection(indicesArray, Mathf.Round(Random.value*(numberOfComponents-1)),correctItem);
}
}
}
function RandomizeArray(arr : Array)
{
for (var i = arr.length - 1; i > 0; i--) {
var r = Random.Range(0,i);
var tmp = arr[i];
arr[i] = arr[r];
arr[r] = tmp;
}
}
private var numberOfComponents:int;
私有变量maxComponents:int=5;
//numberOfComponents返回我的“components”答案库的长度属性
ComponentSelection=buildComponentSelectionList(0)//0是我正确答案的索引
函数构建组件选择列表(correctItemIndex){
var theArray:Array=new Array();
var indicatesofselection:Array=GetIndicatesByInclude(correctItemIndex);
Debug.Log(指示fselection);
对于(var i=0;i您可以循环浏览选项并确定其应包含的概率,然后对包含的选项进行洗牌:
function getRandomOptions(allOptions, correctIndex, count){
var result = [allOptions[correctIndex]];
count--;
var left = allOptions.length;
for (var i = 0; count > 0; i++) {
if (i != correctIndex && Math.floor(Math.random() * left) < count) {
result.push(allOptions[i]);
count--;
}
left--;
}
shuffleArray(result);
return result;
}
function shuffleArray(arr) {
for (var i = arr.length - 1; i > 0; i--) {
var r = Math.floor(Math.random() * i);
var tmp = arr[i];
arr[i] = arr[r];
arr[r] = tmp;
}
}
函数getRandomOptions(allOptions、correctIndex、count){
var结果=[allOptions[correctIndex]];
计数--;
var left=allOptions.length;
对于(变量i=0;计数>0;i++){
if(i!=correctIndex&&Math.floor(Math.random()*左)0;i--){
var r=Math.floor(Math.random()*i);
var tmp=arr[i];
arr[i]=arr[r];
arr[r]=tmp;
}
}
演示:对于初学者,不要在数值索引数组的
循环中使用for…。使用索引变量。好的,我已经将它们转换为索引。我仍然会遇到崩溃,但不再重复!要稍微简化脚本,请使用if(indexarray.indexOf(number)<0)
确定某个项不存在。当您说有时编辑器崩溃时,它是否会告诉您原因?可能是递归方法调用失控了吗?(即,您总是得到doesntExist
值false
).事实上,现在我看了一下,如果数组中有correctItem
,情况不是总是这样吗?直到生成了4个随机索引之后,才将correctItem添加到数组中。我来看看你的建议。