简体   繁体   English

将值随机拼接成数组

[英]randomly splice values into array

I have an array "A" of scrambled, randomly generated ASCII characters... and a message "M". 我有一个加扰的,随机生成的ASCII字符数组“ A”和一条消息“ 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. 我想将消息M的字符插入数组A,以使M的字符顺序完整无缺...但是随机分布在整个数组A中。

Original array: zH$@%@$#@$@^^#@(%*$@^&@!$^%& 原始数组:zH $ @%@ $#@ $ @ ^^#@(%* $ @ ^&@!$ ^%&

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

                    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/ 小提琴: https : //jsfiddle.net/0ftm2srz/1/

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. (!@#!$!@ $#!@#this)但这是随机的。

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. 编辑更好的方法是生成message.length个唯一随机索引,对它们进行升序排序,然后将message中的字符插入加扰数组中的那些位置。

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

 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: 具有上述功能的 第2版

 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 :-) 我认为它仍然不是100%,而是朝着“优化”解决方案发展:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM