Skip to content

Commit

Permalink
more reliable seeded shuffle for lists
Browse files Browse the repository at this point in the history
  • Loading branch information
whitmer committed Jan 10, 2025
1 parent d64f952 commit 3f99a3a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
26 changes: 26 additions & 0 deletions _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -714,4 +714,30 @@
// Avaz, WeaveChat, CBoard, Clicker, GoTalk Now Plus
];
</script>
<script>
// https://stackoverflow.com/questions/16801687/javascript-random-ordering-with-seed
window.shuffle = function(array, seed) { // <-- ADDED ARGUMENT
var m = array.length, t, i;

// While there remain elements to shuffle…
while (m) {

// Pick a remaining element…
i = Math.floor(random(seed) * m--); // <-- MODIFIED LINE

// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
++seed // <-- ADDED LINE
}

return array;
}

function random(seed) {
var x = Math.sin(seed++) * 10000;
return x - Math.floor(x);
}
</script>
</head>
2 changes: 1 addition & 1 deletion certifications/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ bg: 3
var apps = document.getElementById('apps_list');
var other_apps = document.getElementById('other_apps_list');
var app_template = apps.querySelectorAll('.caption')[0];
var list = [].concat(window.app_list || []);
var list = window.shuffle(window.app_list || [], (new Date()).getDate());
if(list.length == 0) {
list.push({name: "None available", desc: " ", rank: 1});
}
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ <h2>Get Involved!
<script>
var apps = document.getElementById('apps_list');
var app_template = apps.querySelectorAll('.caption')[0];
var list = [].concat(window.app_list || []);
var list = window.shuffle(window.app_list || [], (new Date()).getDate());
if(list.length == 0) {
list.push({name: "None available", desc: " ", rank: 1});
}
Expand Down
1 change: 1 addition & 0 deletions vocabularies/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ abrupt, accept, hope, wish, meal, comply
list.push({name: "None available", desc: " ", rank: 1});
}
var day = (new Date()).getDate();
list = window.shuffle(list, day);
var start_num = (day / 31) - 0.5;
if(day % 2 == 0) { start_num = start_num * -1; }
list.forEach(function(item) {
Expand Down

0 comments on commit 3f99a3a

Please sign in to comment.