Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Produce uniformly-distributed permutation in ~jimble #662

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

movq
Copy link

@movq movq commented Jan 23, 2017

The current implementation of ~jimble uses a sorting function with a random comparison function. This does not create a uniform distribution of permutations when used with many (including the default) sorting algorithms.

This patch produces a uniformly-distributed permutation of the word by choosing a random, unused letter from the original word for each position in the jimbled word.


'~jimble': function(event) {
event.reply(event.params[1].split('').sort(function() {
return (Math.round(Math.random()) - 0.5);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This deletion calls Math.random once fewer...

var jimbled = new Array(word.length);
for (var i = 0; i < word.length; i++) {
do {
rnd = Math.floor(Math.random()*word.length);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and this line restores the original code's entropy consumption fingerprint.

unsolicited ACK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants