# Problem with Looping/Recurrence - Null Results [closed]

I'm trying to create a function that maps over a list of bits, and either returns the original bit or "mis-reads" the bit itself. The idea here is to simulate transcription errors.

Here's the function I created:

y := RandomChoice[{0.8, 0.2} -> {0,1}];
transcription[x_] := If[x == y, x, x /. x -> 1];
list = Table[0, {30}];
transcription /@ list


Now, this works great when I run it over the list once. However, what I want to be able to do is iterate the function transcription over my list multiple times, so that over time I can accumulate transcription errors.

The best way I could think of to make this work was a looping construct. However, I've run into some problems with that.

Here's my attempt at a Do-Loop:

 y := RandomChoice[{0.8, 0.2} -> {0, 1}];
transcription[x_] := If[x == y, x, x /. x -> 1];
list = Table[0, {30}];
Print[Do[list = transcription /@ list, {10}]]


But that only returns a Null value, instead of counting up errors.

I also tried a For-Loop:

y := RandomChoice[{0.8, 0.2} -> {0, 1}];
transcription[x_] := If[x == y, x, x /. x -> 1];
list = Table[0, {30}];
Print[For[i = 0, i < 10, i++,
list = transcription /@ list]]


This also created a Null value.

Any help on how to properly set up a looping construct for my purposes would be much appreciated.

-

## closed as off-topic by m_goldberg, Oleksandr R., Sjoerd C. de Vries, Simon Woods, ArtesJul 16 '13 at 10:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, Oleksandr R., Sjoerd C. de Vries, Simon Woods, Artes
If this question can be reworded to fit the rules in the help center, please edit the question.

"Unless an explicit Return is used, the value returned by Do is Null."... and you are printing that Null. Just evaluate list after the loop and see what it is. –  wxffles Jul 16 '13 at 4:32
You want Table[transcription /@ list, {10}] –  m_goldberg Jul 16 '13 at 4:33

This will deliver what you want on a one off and is based on your first block of code:

tmp = Table[y, {30}]
list = MapThread[If[#1 == #2, #1, 1] &, {list, tmp}]


So to figure out what is happening you can cut and paste into different cells and repeat. What we see is the the list list accumulates the 1s -- which I guess are the accumulation of the transcription errors:

tmp = Table[y, {30}]
list = MapThread[If[#1 == #2, #1, 1] &, {list, tmp}]

(*
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1}
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1}
*)

(* next iteration *)
tmp = Table[y, {30}]
list = MapThread[If[#1 == #2, #1, 1] &, {list, tmp}]

(*
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0}
{0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1}
*)


...and so on.

However if that breakdown is correct then there seems no need to overcomplicate this with procedural thinking. Instead this can be coded by just creating a list of your random numbers for as many iterations as you want and total them and clip them. So for 4 iterations the code would be:

Clip@Total[RandomChoice[{0.8, 0.2} -> {0, 1}, {4, 30}]]
(* {1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1} *)

-
RandomVariate[BernoulliDistribution[1 - 0.8^4], 30]

that doesn't deliver the same result as his code when iterating (use SeedRandom to check). You would need to change to Clip@Total[RandomVariate[BernoulliDistribution[0.2], {4, 30}]] –  Mike Honeychurch Jul 16 '13 at 11:30