![](/img/trans.png)
[英]What's the difference between Math.random() >= 0.5 and Math.random() - 0.5
[英]math.random()-0.5 for Multiple Array
我必須使用jquery對3個數組進行排序,但結果的順序應該相同
第一個數組用於文本答案,第二個用於圖像答案,第三個用於音頻答案,在對所有數組進行排序后應按相同的順序排列,但問題更改時必須進行排序
var Textanswers = plugin.config.randomSort || plugin.config.randomSortAnswers ?
question.a.sort(function () { return (Math.round(Math.random()) - 0.5); }) :
question.a;
var Imageanswer= plugin.config.randomSort || plugin.config.randomSortAnswers ?
question.imga.sort(function () { return (Math.round(Math.random()) - 0.5); }) :
question.imga;
var Audioanswer= plugin.config.randomSort || plugin.config.randomSortAnswers ?
question.auda.sort(function () { return (Math.round(Math.random()) - 0.5); }) :
question.auda;
一種方法是使用索引數組,然后改組它。
然后,您可以使用混洗后的數組中的索引以相同的隨機順序訪問其他每個數組的元素。
以下C#代碼演示了此方法(以及標准的隨機播放算法):
using System;
using System.Collections.Generic;
using System.Linq;
namespace Demo
{
class Program
{
static void Main()
{
int[] array1 = {1, 2, 3, 4, 5};
string[] array2 = {"One", "Two", "Three", "Four", "Five"};
double[] array3 = {0.1, 0.2, 0.3, 0.4, 0.5};
// Create an array of indices the same length as the arrays to be shuffled.
var indices = Enumerable.Range(0, array1.Length).ToArray();
// Shuffle the indices.
Shuffle(indices, new Random());
// Now you can use the shuffled indices to access all the arrays in the same random order:
foreach (int index in indices)
{
Console.WriteLine($"{array1[index]}, {array2[index]}, {array3[index]}");
}
}
// Standard shuffle.
public static void Shuffle<T>(IList<T> array, Random rng)
{
for (int n = array.Count; n > 1;)
{
int k = rng.Next(n);
--n;
T temp = array[n];
array[n] = array[k];
array[k] = temp;
}
}
}
}
有很多種方法可以對數組進行混洗,但是我將在此答案中使用您的方法:
const shuffle = xs => xs.sort(() => Math.round(Math.random()) - 0.5));
您的問題包含3個相似長度的數組。 數組中的各項通過索引彼此“相關”:
question.a[i] ~ question.imga[i] ~ question.auda[i]
確保改組時尊重這些關系的一種方法是改組項目的“三重組合”,而不是改組單個數組。
shuffle([
[question.a[0], question.imga[0], question.auda[0]],
/* ... */
])
這就留下了兩個挑戰:1.從多個數組中創建一個相關項的數組2.排序后,從經過改組的三重奏中提取單個數組
讓我們定義一個zip函數:
const zip = (xs, ...others) => xs.map( (x, i) => [x].concat(others.map(ys => ys[i])) ); console.log(zip([1, 2, 3], ["a", "b", "c"], ["I", "II", "III"]));
現在,返回到三個單獨的數組,我們創建一個“問題構造函數”,它使用[a, imga, auda]
數組並將其轉換為具有命名屬性的對象:
const Question = ([a, imga, auda]) => ({ a, imga, auda })
或者,更通用:
const zipped = [[1, "a", "I"], [2, "b", "II"], [3, "c", "III"]]; const unzip = xss => xss.reduce( (t1, t2) => t1.map((x, i) => [].concat(x).concat(t2[i])) ); console.log(unzip(zipped));
const q = { question: "Are my answers shuffled?", answers: [ "Yes", "No", "Maybe" ], audio: [ "yeeeeaaah", "naaaa", "ehhhh" ], img: [ "✔", "⛔️", "❓" ] }; // Utils const zip = (xs, ...others) => xs.map( (x, i) => [x].concat(others.map(ys => ys[i])) ); const unzip = xss => xss.reduce( (t1, t2) => t1.map((x, i) => [].concat(x).concat(t2[i])) ); const shuffle = xs => xs.sort(() => Math.random() - 0.5) // Question shuffler implementation const shuffleQuestion = q => { const [answers, audio, img] = unzip(shuffle(zip(q.answers, q.audio, q.img))); return Object.assign( {}, q, { answers, audio, img } ); }; // Three versions of the question that *might* have // differently ordered answers console.log(shuffleQuestion(q)); console.log(shuffleQuestion(q)); console.log(shuffleQuestion(q));
當然,您也可以創建Matthew提出的方法的javascript實現:
const reorder = order => xs => order.map(i => xs[i]); const range = length => Array.from({ length }, (_, i) => i); const shuffle = xs => xs.sort(() => Math.random() - 0.5); const myShuffle = reorder(shuffle(range(3))); console.log(myShuffle([1, 2, 3])); console.log(myShuffle(["a", "b", "c"])); console.log(myShuffle(["I", "II", "III"]));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.