用随机字母

时间:2015-04-27 03:01:57

标签: javascript jquery arrays string random

我想用letters变量

中的随机字母替换字符串中的任意3个随机字母
var str = "HELLO";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var arr = str.split('');
for (var i = 0; i < 3; i++) {
    var pos = Math.round(Math.random() * arr.length - 1);
    arr.splice(Math.floor(Math.random() * arr.length), 1);
    str = arr.join('');
}
alert(str);

我现在能够取出3个随机字母,但无法弄清楚如何从letters获取3个随机字母并将它们置于随机位置。

这是我现在所拥有的演示。

http://jsfiddle.net/r1zd7rsw/

任何帮助将不胜感激!

4 个答案:

答案 0 :(得分:2)

你可以做到

var str = "HELLO";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var arr = str.split(''),
    //a temp array to store already replaced locations
    temp = [],
    pos, char;
for (var i = 0; i < 3; i++) {
    //since you want 3 different chars to be replaced make sure the current position is not already replaced
    do {
        pos = Math.floor(Math.random() * arr.length);
    } while (temp.indexOf(pos) > -1);

    //find the new char, make sure it is not the same as the current character
    do {
        char = letters[Math.floor(Math.random() * letters.length)]
    } while (arr[pos] == char);

    //replace the character at position pos in the array arr, the character to be replaced is randomly selected from teh letters string
    arr[pos] = char;
    //store the current position in the temp array
    temp.push(pos);
}
str = arr.join('');
console.log(str);

演示:Fiddle

答案 1 :(得分:2)

我尝试了一种更简单的方法:

var str = "HELLO";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var strArray = str.split('');
var lettersArray = letters.split('');

for (var i = 0; i < 3; i++) {
    var pos1 = Math.round(Math.random() * (str.length - 1));
    var pos2 = Math.round(Math.random() * (letters.length - 1));
    strArray[pos1] = lettersArray[pos2];
}

alert(strArray.join(""));

DEMO:https://jsfiddle.net/erkaner/r1zd7rsw/7/

答案 2 :(得分:1)

你快到了!

var str = "HELLO";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var arr = str.split('');
for (var i = 0; i < 3; i++) {
    var pos = Math.round(Math.random() * arr.length - 1);
    var replacementPos = Math.round(Math.random() * letters.length);
    arr.splice(Math.floor(Math.random() * arr.length), 1, letters[replacementPos]);
    str = arr.join('');
}
alert(str);

您所缺少的是对您的其他字符串使用相同的随机位置选择方法,然后使用该位置将替换字符输入您的slice()电话。

从原始内容中更改一些内容,这将更容易阅读:

var str = "HELLO";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var getPos = function(arr) {
    return Math.floor(Math.random() * arr.length);
}

var arr = str.split('');
for (var i = 0; i < 3; i++) {
    arr.splice(getPos(arr), 1, letters[getPos(letters)]);
}
str = arr.join('');
alert(str);

答案 3 :(得分:1)

function randString(num, myStr)
{
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for( var i=0; i < num; i++ ){
        randChar = possible.charAt(Math.floor(Math.random() * possible.length));
        repChar = myStr.charAt(Math.round(Math.random() * myStr.length));
        myStr = myStr.replace(repChar, randChar  );  
    }
    return myStr;
}

 alert(randString(3, "12345"))

样本 http://jsfiddle.net/tuga/5h3jf3v0/2/