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/
-
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 ?Veverke– Veverke2015年02月26日 16:27:48 +00:00Commented 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.lateralus– lateralus2015年02月26日 16:29:06 +00:00Commented Feb 26, 2015 at 16:29
-
You have to post example of the source data and the result.hindmost– hindmost2015年02月26日 16:30:31 +00:00Commented Feb 26, 2015 at 16:30
-
Where is the message string in the given sample?hindmost– hindmost2015年02月26日 16:34:28 +00:00Commented Feb 26, 2015 at 16:34
-
1Seems so veverke growler, Your random should not start from 0 your random should start from the last position you've entered the last letterNeta Meta– Neta Meta2015年02月26日 16:36:44 +00:00Commented Feb 26, 2015 at 16:36
2 Answers 2
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.
8 Comments
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 :-)