Commit d44ffc72 by Иван Кубота

seeded random

parent cb8fbdcd
Pipeline #515 canceled with stage
......@@ -10,6 +10,8 @@
<script src="js/csv.js"></script>
<script src="js/helpers/rand.js"></script>
<script src="js/model/store.js"></script>
<script src="js/model/data.js"></script>
......
const lapk = (text)=>`„${text}“`;
const rand = function(a, b){
if(Array.isArray(a)){
return a[Math.random()*a.length|0];
return a[Math.random.seeded()*a.length|0];
}
if(typeof a === 'object' && 'max' in a){
b = a.max;
......@@ -9,7 +9,7 @@ const rand = function(a, b){
}
a = Math.ceil(a);
b = Math.floor(b);
return Math.floor(Math.random() * (b - a + 1)) + a;
return Math.floor(Math.random.seeded() * (b - a + 1)) + a;
return (r-a)*(b-a)|0;
},
......@@ -19,7 +19,7 @@ const rand = function(a, b){
const item = items[ i ];
sum += item.probability |0;
}
const theRandom = Math.random();
const theRandom = Math.random.seeded();
let total = 0;
for( let i = 0, _i = items.length; i < _i; i++ ){
......@@ -46,7 +46,7 @@ const quizTypes = {
};
const shuffle = function (a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const j = Math.floor(Math.random.seeded() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
......
function mulberry32(a) {
var out = function() {
var t = a += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
out.getSeed = function() {
return a;
};
out.setSeed = function(A) {
a = A;
};
out.setStringSeed = function(str) {
str = str.replace(/[^0-9a-z]/g,'');
if(str.length === 0)str = '1';
a = parseInt(str,36);
};
out.getStringSeed = function() {
return a.toString(36);
};
return out;
}
Math.random.seeded = mulberry32(Math.floor(Math.random()*4294967296));
\ No newline at end of file
......@@ -14,6 +14,8 @@ view.page.Generate = function() {
type = store.get('generateType');
try{
seedInput.value = Math.random.seeded.getStringSeed();
const result = quizGenerator( type, photo );
title.innerHTML = textFormat( result.question, true );
......@@ -26,7 +28,8 @@ view.page.Generate = function() {
}
};
let title, answers, debug;
let title, answers, debug, seedInput, setSeed;
this.dom =
div({cls: 'generate-panel'},
div({cls: 'title-gradient'},
......@@ -45,7 +48,17 @@ view.page.Generate = function() {
div({cls: 'generate-controls'},
D.input({
attr: {type: 'button', value: 'F5'},
on: {click: update}})),
on: {click: update}}),
span({cls: 'generate-seed-label'}, 'Seed:'),
seedInput = D.input({
cls: 'seed',
attr: {type: 'input', value: '', placeholder: 'seed'},
on: {change: function() {
Math.random.seeded.setStringSeed(seedInput.value);
}}}),
),
div({cls: 'generate-example'},
title = div({cls: 'generate-title'}),
......
......@@ -226,3 +226,6 @@ textarea.generate-debug {
.generate-controls {
margin:16px
}
.generate-seed-label {
margin: 0 8px 0 32px
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment