I would like to ask for help in a Random Replace of character. For example:

ORANGE and change 1 character randomly (random place and random character from ORANGE) in it into any character of ORANGE, for example OOANGE or ORNNGE, etc... All must be UpperCase.

Can anyone help me, please?

  • 1
    $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ Commented Nov 19, 2015 at 19:39

5 Answers 5

  , RandomChoice[Characters@#]
  , {#, #} &@RandomInteger[{1, StringLength@#}]
 ] &@"ORANGE"

This will change the first occurrence of a random char by another random char

f[s_] := StringReplace[s, Rule @@ RandomChoice[Characters@s, 2], 1]

(* "ORRNGE"*)

Here's a slightly more involved approach that always changes a letter, or signals an error:


MutateString::nomut = "All characters in string `` are the same.";

MutateString[s_String] := 
 With[{choices = DeleteDuplicates[Characters[s]]}, 
  With[{n = RandomInteger[{1, StringLength[s]}]}, 
   StringReplacePart[s, RandomChoice[DeleteCases[choices, 
       StringTake[s, {n}]]], {n, n}] /; 2 <= Length[choices]]]

MutateString[s_String] /; Message[MutateString::nomut, s] || False := Null;

It's a little slow, but we can use it to see how many mutations it takes to turn all the characters in "ORANGE" the same:

mutationsUntilAllSame =
      NestWhileList[Check[MutateString[#], $Failed] &, "ORANGE", 
       Not@*FailureQ, 1] - 2,




Experience shows that in order to understand a text it is by far not necessary that all ist letters to be correct. Here's a little game to experiment with it.

We start with this text from Wikipedia:

t = "Mathematica is a symbolic mathematical computation program, \
sometimes called a computer algebra program, used in many scientific, \
engineering, mathematical, and computing fields. It was conceived by \
Stephen Wolfram and is developed by Wolfram Research of Champaign, \
Illinois. The Wolfram Language is the programming language used in \

Then define the replacement function slightly different from Belisarius in that each randomly generated replacement is carried out on each occurence in the whole text.

f[s_] := StringReplace[s, Rule @@ RandomChoice[Characters@s, 2]]

And then we repeat the replacements $n$ times using Nest. Here we go

This Looks still English

Nest[f, t, 1]

"Mathematica is symbolic mathematical computatioe program, sometimes called a computer algebra program, used ie maey scieetific, eegieeerieg, mathematical, aed computieg fields. It was coeceived by Stephee Wolfram aed is developed by Wolfram Research of Champaige, Illieois. The Wolfram Laeguage is the programmieg laeguage used ie Mathematica."

With two replacements it starts to get difficult

Nest[f, t, 2]

"Mlthellticl is sylbolic llthellticll colputltion progrll, soletiles cllled l colputer llgebrl progrll, used in llny scientific, engineering, llthellticll, lnd colputing fields. It wls conceived by Stephen Wolfrll lnd is developed by Wolfrll Reselrch of Chllplign, Illinois. The Wolfrll Llngulge is the progrllling llngulge used in Mlthellticl."

At n = 3 a bit of Gaelic flavour develops

Nest[f, t, 3]

"Mllhemllicl is symbolic mllhemllicll compulllion progrlm, somelimes cllled l compuler llgebrl progrlm, used in mlny scienlific, engineering, mllhemllicll, lnd compuling fields. Il uls conceived by Slephen Wolfrlm lnd is developed by Wolfrlm Reselrch of Chlmplign, Illinois. The Wolfrlm Llngulge is lhe progrlmming llngulge used in Mllhemllicl."

And n = 5 is already unintelligible

Nest[f, t, 5]

"Mvthelvt cv s sslbol c lvthelvt cvl colputvt on progrvl, solet les cvlled v colputer vlgebrv progrvl, used n lvns sc ent f c, eng neer ng, lvthelvt cvl, vnd colput ng f elds. It wvs conce ved bs Stephen Wolfrvl vnd s developed bs Wolfrvl Resevrch of Chvlpv gn, Ill no s. The Wolfrvl Lvnguvge s the progrvll ng lvnguvge used n Mvthelvt cv."

  • 1
    $\begingroup$ Except for the words "Stephen" and "developed"! $\endgroup$
    – march
    Commented Nov 20, 2015 at 6:32
  • 1
    $\begingroup$ At n = 5 it switches from Gaelic to Breton. $\endgroup$
    – shrx
    Commented Nov 20, 2015 at 9:35
  • $\begingroup$ @march this can't have happened accidentally ;-) $\endgroup$ Commented Nov 20, 2015 at 11:05
  • $\begingroup$ @shrx I even have found examples of Nahuatl (native language of the Aztecs). $\endgroup$ Commented Nov 20, 2015 at 11:07

A simple solution that excludes self-replacement.

  StringReplacePart[s, StringTake[s, 
     ConstantArray[#[[1]], 2]], 
     ConstantArray[#[[2]], 2]] &@ 
       RandomSample[Range@StringLength@s, 2]]@"ORANGE"

(note you could end up with the same string in the case of repeated characters in the input)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.