5
$\begingroup$

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?

$\endgroup$
  • 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$ – Dr. belisarius Nov 19 '15 at 19:39
7
$\begingroup$
StringReplacePart[
  #
  , RandomChoice[Characters@#]
  , {#, #} &@RandomInteger[{1, StringLength@#}]
 ] &@"ORANGE"
$\endgroup$
6
$\begingroup$

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

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

(* "ORRNGE"*)
$\endgroup$
4
$\begingroup$

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

ClearAll[MutateString];

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 =
  Table[
   Quiet[
    Length@
      NestWhileList[Check[MutateString[#], $Failed] &, "ORANGE", 
       Not@*FailureQ, 1] - 2,
    MutateString::nomut],
   {10000}];

Histogram[
 mutationsUntilAllSame,
 Automatic,
 "PDF"]

mutations

$\endgroup$
4
$\begingroup$

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 \
Mathematica.";

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."

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

A simple solution that excludes self-replacement.

Function[{s}, 
  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)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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