使用分数的测验生成器

时间:2017-03-19 20:39:31

标签: javascript arrays

我是一名数学老师,需要一种方法来创造单词问题并测试学生这些问题。因为我希望能够重新测试这些学生,我需要一种方法来重新创建相同的问题,并根据给出的问题显示每次不同的答案选择。因此,我使用数组来保存变量信息,例如名称位置或出现在单词问题中的食物。

Ex dave星期一有1个苹果。戴夫去商店买了3个苹果。戴夫有多少苹果?

所以目前我能够创建一个名称数组,水果数组,位置数组,以便在每次询问问题时替换(名称水果或位置)。

我的问题是如何在答案和可能的选择中添加分数?我的第二个问题是我需要一种在答案中提供数字和单词的方法。我附上了我想要创建的问题的例子。

目前,我有多种选择和开放式答案,可以根据实际答案添加随机数量。

enter image description here

1 个答案:

答案 0 :(得分:0)

不过,但是你的代码非常混乱和冗余,我不想透过这些代码。

所以我做了一个简单的例子,利用PRNG(Mersenne Twister)生成“随机”句子

//I'll add the non minified version
prng=function(){"use strict"
function n(r,t){function e(){var n=r,e=t,u=n[e],o=n[t=623>e?e+1:0],i=n[227>e?e+397:e-227]
return n[e]=i^(2147483648&u|2147483647&o)>>>1^(1&o&&2567483615),u^=u>>>11,u^=u<<7&2636928640,u^=u<<15&4022730752,u^=u>>>18,u>>>0}var u={uint32:e,float(){return e()/4294967296},double(){return(e()>>>6)/67108864+(e()>>>5)/9007199254740992},seed(n){var e=n>>>0||5489,o=1
for(r[0]=e,t=0;624>o;++o)e^=e>>>30,e=(r[o]=(1812433253*(e>>>16)<<16)+1812433253*(65535&e)+o)>>>0
return u},clone(){return n(new Uint32Array(r),t)}}
return u}return n(new Uint32Array(624),0).seed(4294967296*Math.random()).clone}()

//the actual application
var target = document.querySelector('#sentences');
var seed = document.querySelector('#seed');
var numRows = document.querySelector('#numRows');
seed.onchange = numRows.onchange = update;



const protagonists = ['Beth', 'Chris', 'Carl', 'April', 'Cinnamon','Ethan', 'Sammy', 'Dan', 'Devron', 'Livron', 'Paul', 'the Frog', 'the Dog', 'the Hamster'];

const actions = ['saw', 'walked to', 'jumped onto', 'hit', 'got bitten by', 'kissed'];

//create a new prng
var random = prng();

//a utility
function sample(array){
	return array[ Math.floor(random.float() * array.length) ];
}

function update(){
	//re-seed the prng
  random.seed(+seed.value);
  
  //flush the seeded values out of the buffer
  for(var i=1234; i--; random.float());
  
  var rows = Array(+numRows.value);
  for(var i = 0; i<rows.length; ++i){
  	var a = sample(protagonists), 
    	b = sample(protagonists),
      action = sample(actions);
      
    while(a === b) b = sample(protagonists);
    
    rows[i] = `<li>${a} ${action} ${b}</li>`;
  }
  
  target.innerHTML = `<ul>${ rows.join("\n") }</ul>`;
}

update();
<label>
  Seed for the PRNG: 
  <input id="seed" type="number" min=0 step=1 value="42"/>
</label>
<br>
<label>
  number of generated rows: 
  <input id="numRows" type="number" min=0 step=1 value="5"/>
</label>

<div id="sentences"></div>

使用输入播放一下。这个例子的目的是展示这个prng如何为同一个种子生成相同的“随机”表达式。

如果您查看代码,yuu会看到id不会缓存任何句子,每次更改其中一个输入时都会从头开始生成。

prng的未缩小版本:

prng = (function(){
    'use strict';

    //implementing a mersenne-twister with 32bit 
    function _mt19937(buffer, i){
        function uint32() {
            var $ = buffer,
                j = i,
                a = $[j], 
                b = $[i = j<623? j+1: 0],
                c = $[j<227? j+397: j-227];

            $[j] = c ^ ((0x80000000 & a | 0x7FFFFFFF & b) >>> 1) ^ (b&1 && 0x9908b0df);

            a ^= a >>> 11;
            a ^= (a << 7) & 0x9d2c5680;
            a ^= (a << 15) & 0xefc60000;
            a ^= a >>> 18;
            return a >>> 0;
        }
        var me = {
            //0 <= uint32() < 4294967296
            uint32,

            //0 <= float() < 1 with 32bit precision
            float(){
                return uint32() / 4294967296;
            },

            //0 <= double() < 1 with 53bit precision (throwing 11 bit away)
            double(){
                return (uint32()>>>6) / 67108864 + (uint32()>>>5) / 9007199254740992;
            },

            //(re-)seed the internal state
            seed(seed){
                var a = seed>>>0 || 5489, j = 1;
                for(buffer[0] = a, i = 0; j<624; ++j){
                    a ^= a>>>30;
                    a = (buffer[j] = ((0x6c078965 * (a >>> 16)) << 16) + 0x6c078965 * (0xffff & a) + j)>>>0;
                }
                return me;
            },

            //create a new instance that has the same state.
            //states are not shared, they develop individually
            clone(){
                return _mt19937(new Uint32Array(buffer), i);            
                //return _mt19937(buffer.slice(), i);           
            }
        }

        return me;
    }

    //create a prototype, seed it with a random number (per page and request)
    //each generated instance will start as a clone of this, 
    //and return the same sequence
    //unless you (re-)seed them
    return _mt19937(new Uint32Array(624),0).seed( Math.random() * 4294967296 ).clone;
    //return _mt19937(new Array(624),0).seed( Math.random() * 4294967296 ).clone;
})();