0

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?

3
  • I'll make a quick example for you. Commented Mar 23, 2013 at 22:06
  • I added my answer + a working example, please tell me if this is what you needed. Commented Mar 23, 2013 at 22:17
  • rewritten my answer on JS =) Commented Mar 23, 2013 at 23:39

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Zachrip. This is good but not exactly what I was looking for. I am looking replace random individual chars from givenString with randomly picked chars from charToTakeFrom (array or string.)
@RamIyer Oh okay, that'd be pretty easy as well. I can make up another example(it'll be just tweaked a little bit)
@RamIyer Updated, hope that's what you meant.
1

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

Comments

0

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.

Comments

Your Answer

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.