0

I have an array "A" of scrambled, randomly generated ASCII characters... and a message "M". I want to insert the characters of message M into array A such that the order of M's characters are intact... but randomly distributed throughout array A.

Original array: zH$@%@$#@$@^^#@(%*$@^&@!$^%&

Sample output: zH$@%@^t$#@$@^^h#@(%*$@^&@i!$^%&s, etc...

 var randomChars = [];
 for(var i=33;i<127;++i) {
 var letter = document.createElement('span');
 letter.innerHTML = String.fromCharCode(i);
 randomChars.push(letter);
 }
 var message = "this is a message";
 var rand = 0;
 for (var i = 0; i < message.split("").length; i++) { 
 rand = Math.floor((Math.random() * randomChars.length) + rand);
 var letters = document.createElement('span');
 letters.innerHTML = message.split("")[i];
 letters.setAttribute("hidden","");
 randomChars.splice(rand, 0, letters); 
 }

Fiddle: https://jsfiddle.net/0ftm2srz/1/

asked Feb 26, 2015 at 16:25
9
  • I do not understand what you mean with "order of the message is maintained". Once you scramble the letters in the array, what's next ? Commented Feb 26, 2015 at 16:27
  • what do you mean by randomly scatter mantaining the order of the message? It doesn't make much sense like this. Commented Feb 26, 2015 at 16:29
  • You have to post example of the source data and the result. Commented Feb 26, 2015 at 16:30
  • Where is the message string in the given sample? Commented Feb 26, 2015 at 16:34
  • 1
    Seems so veverke growler, Your random should not start from 0 your random should start from the last position you've entered the last letter Commented Feb 26, 2015 at 16:36

2 Answers 2

3

Use the previous random index as the minimum (non inclusive) of your next randomly generated index. Start at zero.

You could end up with some barely scrambled stuff, though. (!@#!$!@$#!@#this) But it's random.

EDIT A better way would be to generate a message.length amount of unique random indices, sort them in ascending, and then insert characters from message at those spots in the scrambled array.

http://jsbin.com/kuzepujabo/1/edit?js,console

answered Feb 26, 2015 at 16:36
Sign up to request clarification or add additional context in comments.

8 Comments

I had thought about this before too... I'm wondering why the letters are all clumping up at the end- the randomly generated numbers are beyond the length of the array
That would be randomly selected and in order, though. If the first letter of your message gets placed near the end of the array then all the rest of the letters must come after, and so you get the "clump". Does it have to be random? You could make a seemingly random looking distribution by dividing your target array into a section for each letter to place, then randomly placing a letter in each section, and recombining. It's not really random, but there's some randomness to it. You would also have a somewhat even distribution.
"Random" and "in order" are contradictory, so to have things in order you're going to sacrifice 'randomness' to some degree.
"Random" and "in order" are contradictory if referring to the same thing. But in this case, Random refers to the index at which a letter of the message M is inserted into array A, and In Order refers to the preserving the index order of each letter in message M such that... if a letter x in message M has index 0, and a letter y in message M has index 1, x will always be inserted into array A before y.
They are still related. The index in which you insert your letters from M must be partially determined from where the previous letters were placed. Your example illustrates that.
|
0

var o = {
 array: "zH$@%@$#@$@^^#@(%*$@^&@!$^%&".split(''),
 msg: "this is a message",
 randomMsgIndex: function () { return Math.floor(Math.random() * this.msg.length); },
 randomMsgChar: function () { return this.msg[this.randomMsgIndex()]; },
 //resultingArray: [],
 randomArrayIndex: function () { return Math.floor(Math.random() * this.array.length); }
}
for(var i = 0; i < o.msg.length; i++) {
 o.array.splice(o.randomArrayIndex(), 0, o.randomMsgChar());
}
console.log(o.array);

I have come up with this - but I assume it is still not what you want - you probably want something that keeps track of which message chars were already added - so not to add them twice - and make sure the entire message (all its characters) were added to the array.

Version 2 with the feature described above:

var o = {
 array: "zH$@%@$#@$@^^#@(%*$@^&@!$^%&".split(''),
 msg: "this is a message",
 msgArray: function () { this.msg.split(''); },
 randomMsgIndex: function () { return Math.floor(Math.random() * this.msg.length); },
 randomMsgChar: function (i) { return this.msg[i]; },
 //resultingArray: [],
 randomArrayIndex: function () { return Math.floor(Math.random() * this.array.length); },
 stripStr: function (indexToSkip, originalStr) { 
				var result = "";
				for (var i = 0; i < originalStr.length; i++)
					if (indexToSkip != i)
					 result += originalStr[i];
				return result;
 }
}
for(var i = 0; i < o.msg.length; i++) {
 var msgRandomIndex = o.randomMsgIndex();
 o.array.splice(o.randomArrayIndex(), 0, o.randomMsgChar(msgRandomIndex));
 o.msg = o.stripStr(msgRandomIndex, o.msg);
}
console.log(o.array);

I think it it is still not a 100%, but moving towards the "optimized" solution :-)

answered Feb 26, 2015 at 17:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.