简体   繁体   English

随机 - 为测验中的错误答案添加偏见(javascript)

[英]random - adding bias to incorrect answers in a quiz (javascript)

I wanted to make a relatively simple algorithm, that isn't just randomly choosing a question.我想做一个相对简单的算法,而不仅仅是随机选择一个问题。 So for each question I have a value called numOfCorrect, when the user gets the answer correct it adds 1, if the user gets it wrong it subtracts 1. like this:因此,对于每个问题,我都有一个名为 numOfCorrect 的值,当用户得到正确答案时,它加 1,如果用户答错,则减 1。像这样:

const questions = [
    {question: "what is 9 + 10?", numOfCorrect: -5 ...},
    {question: "what is the meaning of life?", numOfCorrect: -5 ...},
    {question: "how do I get a bias for the incorrect?", numOfCorrect: 0 ...},
    {question: "1 + 1 = 4?", numOfCorrect: 10 ...}

]

so if each numOfCorrect is 0, I would want each question to have an even chance of getting picked, so that's just picking a random question normally.所以如果每个 numOfCorrect 都是 0,我希望每个问题都有被选中的机会,所以这只是正常选择一个随机问题。

The questions will be sorted from the least numOfCorrect to the most.问题将从最少 numOfCorrect 到最多排序。 I don't want to just decrease the range to bias the first indexes.我不想只是减小范围以偏向第一个索引。 Instead I want the probability for the questions to increase or decrease based on the numOfCorrect value, so there should be a non zero chance for any of the questions to be picked.相反,我希望问题的概率根据 numOfCorrect 值增加或减少,因此选择任何问题的机会都应该是非零的。

I was thinking something like:我在想类似的事情:

  • for a question with a negative numOfCorrect => 1 / questions.length * ( 1 + ( (1/question.numOfCorrect) * -1)), so for a value -5 it would be 0.25 * 1.2 which would be 0.3.对于一个负数 numOfCorrect => 1 / questions.length * ( 1 + ( (1/question.numOfCorrect) * -1)) 的问题,因此对于值 -5,它将是0.25 * 1.2 ,即 0.3。 So instead of 25% chance it would be 30%.所以不是 25% 的机会,而是 30%。

  • for a question with positive numOfCorrect.对于 numOfCorrect 为正的问题。 If numOfCorrect was 1, then it would be 0.25 * 0.9, 2 would be 0.25 * 0.8...如果 numOfCorrect 为 1,则为 0.25 * 0.9,2 为 0.25 * 0.8...

Problem is they won't add to 1 (obviously).问题是它们不会加到 1(显然)。 I don't know what I'm doing, I'm no mathematician.我不知道我在做什么,我不是数学家。

So main question.所以主要问题。 How can pick a random question and bias the ones with a lower numOfCorrect, that will preferably exponentially increase/decrease their probability of being picked as the value numOfCorrect increases/decreases??如何选择一个随机问题并偏向具有较低 numOfCorrect 的问题,这将优选地随着 numOfCorrect 值的增加/减少而以指数方式增加/减少他们被选择的概率?

  1. Generate a (positive) weight for each question with a formula based on numOfCorrect (no need to consider questions.length in the formula).使用基于numOfCorrect的公式为每个问题生成(正)权重(无需考虑公式中的questions.length )。

  2. Sum up the weights.总结权重。

  3. Generate a random number between 0 and the sum of the weights.生成一个介于 0 和权重之和之间的随机数。

  4. Loop over the questions and pick one like this:循环问题并选择一个这样的问题:

// randomValue is between 0 and the sum of the weights

let totalWeightSoFar = 0;
for (let i = 0; i < questions.length; i++) {
    let question = questions[i];
    if (randomValue >= totalWeightSoFar && randomValue < totalWeightSoFar + question.weight) {
        // found it!
        return question;
    }

    totalWeightSoFar += question.weight;
}

UPDATE:更新:

Tes, it does mean that the weights can't be negative. Tes,这确实意味着权重不能为负。

Imagine that you put sheets of papers of different sizes and different colors next to each other.想象一下,您将不同尺寸和不同 colors 的纸张并排放置。 Then toss a coin so that it lands on a perfectly-random spot in the area covered by the papers.然后投掷一枚硬币,使其落在纸张覆盖区域的一个完全随机的位置。

The chance of the coin landing on a specific color is proportional to the size of the paper of that color compared to the sum of the sizes of all the papers together (ie the total area covered by papers).硬币落在特定颜色上的机会与该颜色的纸张尺寸与所有纸张尺寸的总和(即纸张覆盖的总面积)成正比。

For example, if the area is comprised of a blue paper and a red paper twice its size - then the blue would cover 1/3 of the area, and the red would cover 2/3.例如,如果该区域由一张蓝纸和一张两倍大小的红纸组成 - 那么蓝色将覆盖该区域的 1/3,红色将覆盖 2/3。 The chance of the coin landing on blue would be 1/3 (ie ~33%).硬币落在蓝色上的机会是 1/3(即~33%)。

Similarly, if your array has two questions, and one is assigned a weight of 7 (I'm just giving an arbitrary number), and the other is assigned a weight of 14 - the chances of the random number "falling" between 0 and 6 would be ~33%, and the chances of it falling between 7 and 20 would be ~66%).同样,如果您的数组有两个问题,其中一个的权重为 7(我只是给出一个任意数字),另一个的权重为 14 - 随机数“落在”0 和6 约为 33%,它落在 7 到 20 之间的几率约为 66%)。

How "sensitive" the weight is and whether it's affected by the number of questions depends on how you calculate it.权重有多“敏感”以及是否受问题数量的影响取决于您的计算方式。 This just depends on your requirements.这仅取决于您的要求。

For example, you could define the weight as 100 + numOfCorrect .例如,您可以将权重定义为100 + numOfCorrect In such case, if all questions where answered correctly the same number of times - the weights would be equal, and the coin would have the same probability of landing on any of the questions.在这种情况下,如果所有问题的正确回答次数相同 - 权重将相等,并且硬币将具有相同的概率落在任何问题上。

BTW, if it simplifies your calculation logic, you could allow negative weights, but just offset all weights by the absolute value of the smallest weight afterwards.顺便说一句,如果它简化了您的计算逻辑,您可以允许负权重,但只需用之后最小权重的绝对值来抵消所有权重。 Eg if you end up with weights [ 2, -37, 84, -12 ] , then you find the minimum (ie -37 ) and add it to each of the weights, resulting in [ 39, 0, 121, 25 ] .例如,如果您最终得到权重[ 2, -37, 84, -12 ] ,那么您会找到最小值(即-37 )并将其添加到每个权重中,从而得到[ 39, 0, 121, 25 ] Then you sum up these weights and run the loop.然后你总结这些权重并运行循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM