From 8ef73aeeeb8a21856889e710f19661f69d4d2133 Mon Sep 17 00:00:00 2001 From: Michael Jones Date: Mon, 23 Jan 2017 03:11:33 +0000 Subject: [PATCH] Produce uniformly-distributed permutation in ~jimble --- modules/words/words.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/modules/words/words.js b/modules/words/words.js index 92014aa..d367ab4 100644 --- a/modules/words/words.js +++ b/modules/words/words.js @@ -102,11 +102,20 @@ var words = function(dbot) { } }); }, - - '~jimble': function(event) { - event.reply(event.params[1].split('').sort(function() { - return (Math.round(Math.random()) - 0.5); - }).join('')); + + '~jimble': function(event) { + var word = event.params[1].split(''); + var used = []; + var jimbled = new Array(word.length); + for (var i = 0; i < word.length; i++) { + do { + rnd = Math.floor(Math.random()*word.length); + } while (used.indexOf(rnd) != -1); + + jimbled[i] = word[rnd]; + used.push(rnd); + } + event.reply(jimbled.join('')); }, '~merge': function(event) {