Ram Iyer
Ram Iyer

Reputation: 1564

How to replace random chars in a string in javascript

I would like to replace chars randomly picking from another string...

var charToTakeFrom = '0x080,0x081,0x082,0x083,0x084,0x085,0x086,0x087,0x088,0x089,0x090';
var givenString = 'The brown fox jumps over the pink fence.';

Resultant string should replace random chars in givenString with random chars from charToTakeFrom string.

The simplest way is to write a for loop to replace random number of chars in givenString with random chars from charToTakeFrom.

Is there an easier/faster method?

Upvotes: 0

Views: 3906

Answers (3)

Zachrip
Zachrip

Reputation: 3382

Whole Words: Example

var ctu = [],s = "The bird flew majestically across the valley in search of worms.", newstring=" ";
ctu[0]="derp4561";
ctu[1]="derp12534";
ctu[2]="derp684";
var s = s.split(" ");
for(var i = 0;i<s.length;i++){      
s[i]=ctu[Math.floor(Math.random() * ctu.length)];
newstring +=s[i] + " ";
}
document.write(newstring);

Single Characters: Example

var ctu = [],s = "The bird flew majestically across the valley in search of worms.", newstring="";
ctu[0]="0x080";
ctu[1]="0x081";
ctu[2]="0x082";
ctu[3]="0x083";
ctu[4]="0x084";
ctu[5]="0x085";
var s = s.split("");
for(var i = 0;i<s.length;i++){
if(s[i] != " "){
    s[i]=ctu[Math.floor(Math.random() * ctu.length)];
    newstring +=s[i];
}
else{
    newstring += " ";
}
}
document.write(newstring);

Upvotes: 2

Denis O.
Denis O.

Reputation: 1850

I'm not sure if it is the fastest way, but anyway its gicky enough =) hope you like it

EDIT2 Done on JS!

var complexity = 0.3; // 0 to .99 if more then more letters in src string will be replaced by random ones 
var randsArr = ("!@#$)**($#*)($#$()$()#$%^&*()%^$#$$#$^").split('').sort(function () { return 0.5 - Math.random()});
var srcArr = ("The brown fox jumps over the pink fence.").split('');
result = $.map(srcArr,function (el) { return (Math.random()>complexity) ? el : (randsArr.length) ? randsArr.shift() : el ; }).join('');

Test it on JSFiddle

Old php version, just in case someone need it:

$rands = "!@#$%^&*()";
$srcStr = "The brown fox jumps over the pink fence.";
$srcArr = str_split ($srcStr);
$randsArr = array_merge(str_split($rands),array_fill(strlen($rands),strlen($srcStr)-strlen($rands),false));
shuffle($randsArr);
echo implode(array_map(function($letter,$replacement) {  return ($replacement!=false)?$replacement:$letter;  },$srcArr,$randsArr));

EDIT My bad... I was thinking it's about php =( Give me couple minutes to rewrite it on JS

Upvotes: 1

aIKid
aIKid

Reputation: 28292

First, you better use arrays. And then:

var charToTakeFrom = [0x080,0x081,0x082,0x083,0x084,0x085,0x086,0x087,0x088,0x089,0x090];
var givenString = 'The brown fox jumps over the pink fence.';
var randomNumber = Math.floor((Math.random() * 10)) + 1); //Computes a random number from 1 to 11
var slicer = randomNumber - 1;
var slice = charToTakeFrom.slice(slicer, randomNumber); //get the character

I assume you already know how to replace a string.

Upvotes: 0

Related Questions