Data talk:Fraction tasks random chart values.tab

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

See also Data talk:Fraction tasks random chart values div2.tab.

JavaScript source for this Data Table:

const primes= [2,3,5,7,11,13,17,19,23]
function ggT(a, b) {
  var facs=[]
  for(var j = 0; j<20; j++)
    for(var i = 0; i < primes.length; i++) 
    if (a%primes[i]==0 && b%primes[i]==0) {
      facs.push(primes[i])
      a= a/primes[i]; b= b/primes[i];
      break
    }
  var ggt=1
  for(var i = 0; i < facs.length; i++)
    ggt*= facs[i]
  return ggt
}
function erfinv(x) {
  return 0.5*Math.sqrt(3.14)
    *(x +3.14/12*x**3 +7*3.14**2/480*x**5 +127*3.14**3/40320*x**7 +4369*3.14**4/5806080*x**9)
}
//movebias is 1.2 (0-bias) to 1.4 (1-bias)
function biasedRand2( movebias=1.3 ) {
  var r= erfinv(Math.random()*2.6-movebias)
  return (r- erfinv(0 *2.6-movebias)) / (erfinv(1 *2.6-movebias) - erfinv(0 *2.6-movebias))
}
var gap= 0.4 //blocks
function wholeBlocksInWidth(wholeWidth, width) {
  return Math.floor((width + gap) / (wholeWidth + gap))
}
function makeValFrac(maxval) {
  var maxden= 12
  var denom= Math.floor(Math.random() * (maxden-1)) + 2
  var nom= Math.floor(biasedRand2() * denom) + 1 +(maxval-1)*denom //min=maxval-1

  var nextWhole= Math.ceil(nom/denom)
  var width1=  denom%1!=0? 99: denom*nextWhole/1+(nextWhole-1)*gap
  var width2=  denom%2!=0? 99: denom*nextWhole/2+(nextWhole-1)*gap
  var width3=  denom%3!=0? 99: denom*nextWhole/3+(nextWhole-1)*gap
  var T= 1
  if (ggT(nom,denom)>1 && Math.random()<0.8) {
    if (nom%2==0 && denom%2==0 && Math.random<0.7) T= 2
    if (nom%3==0 && denom%3==0 && Math.random<0.7) T= 3
    T= ggT(nom,denom)
  }                                 //redFr: nom/T / denom/T
  var wholes= Math.floor(nom/denom) //redWholes: wholes
  var nomMixFr= nom-denom*wholes    //redMixFr: wholes + nomMixFr/T / denom/T
  var redFr= denom/T==1? ''+nom/T: '\\\\frac{'+nom/T+'}{'+denom/T+'}'
  var mixFr= nomMixFr==0? ''+wholes: (wholes!=0?wholes:'') +'\\\\frac{'+nomMixFr+'}{'+denom+'}'
  var redMixFr= nomMixFr==0? ''+wholes: (wholes!=0?wholes:'') +'\\\\frac{'+nomMixFr/T+'}{'+denom/T+'}'
  var val= (nom/denom).toFixed(2)/1
  var s= [val, nom, denom, width1, width2, width3, 
    '"'+redFr+'"', '"'+mixFr+'"', '"'+redMixFr+'"', T, nom%denom==0].join(', ')
  return {s:s, val:val, T:T, whole:nom%denom==0}
}
function makeVFsWith(condF, maxval, num) {
  var o= []
  for(var j = 1; j <= num; j++) {
  var s= "err"
  for(var i = 1; i <= 10; i++) {
    var fr= makeValFrac(maxval)
    if (condF(fr)) {
      s= fr.s
      break
    }
  }
  o.push({s:s, rand:Math.random()})
  }
  return o
}
//==Main
var l= []; var ls= []; var id= 1; var n= 100
l=       (makeVFsWith(function(a) {return a.whole==false}, 1, n*0.7))
  .concat(makeVFsWith(function(a) {return true          }, 1, n*0.3))
  .sort(function(a, b) {return a.rand<b.rand})
for(var i= 0; i<l.length; i++) {
  ls[id-1]= '    ['+(id++)+', '+l[i].s+']'
}
l=       (makeVFsWith(function(a) {return a.whole==false}, 2, n*0.7))
  .concat(makeVFsWith(function(a) {return true          }, 2, n*0.3))
  .sort(function(a, b) {return a.rand<b.rand})
for(var i= 0; i<l.length; i++) {
  ls[id-1]= '    ['+(id++)+', '+l[i].s+']'
}
l=       (makeVFsWith(function(a) {return a.whole==false}, 3, n*0.7))
  .concat(makeVFsWith(function(a) {return true          }, 3, n*0.3))
  .sort(function(a, b) {return a.rand<b.rand})
for(var i= 0; i<l.length; i++) {
  ls[id-1]= '    ['+(id++)+', '+l[i].s+']'
}
l=       (makeVFsWith(function(a) {return a.whole==false}, 3, n*0.2))
  .concat(makeVFsWith(function(a) {return a.whole==false}, 3, n*0.2))
  .concat(makeVFsWith(function(a) {return a.whole==false}, 2, n*0.2))
  .concat(makeVFsWith(function(a) {return true          }, 2, n*0.3))
  .concat(makeVFsWith(function(a) {return true          }, 1, n*0.1))
  .sort(function(a, b) {return a.rand<b.rand})
for(var i= 0; i<l.length; i++) {
  ls[id-1]= '    ['+(id++)+', '+l[i].s+']'
}
console.log(ls.join(', \n'))

--Neuneinhalbtel (talk) 17:52, 6 August 2021 (UTC)[reply]