0

I need to apply this experiment:

For all numbers between [0,128] let be t
{set total=0

repeat this experiment n times

{set start=0 choose random number [0,255] lets be R

While(R!=t)
{shift R by multiply by 2 and take the reminder on 256
choose randomly a bit and added to R

increase start by one}

add start to total }

print(total/n)

}

if any body can help me applying this algorithm,thanks in advance this is my code it's not working at all

For[i = 0, i < 10,   
i++ {a = 0; 
For[j = 0, j < n,    
j++, {b = 0; d = RandomInteger[127]; Print[d];    
While[d != i, {b = b + 1; c = RandomInteger[];    
d = Mod[d*2, 256] + c}]} a = a + b]}]
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
math.
  • 59
  • 3

1 Answers1

2

This may be an answer.

Your code is ill-formed. It does not conform to Mathematica syntax. I have rewritten it to be valid and to better correspond to your pseudocode. There is no guarantee that it actually corresponds to the process you want to simulate, but at least it prints output and terminates. I hope it will help you to move forward.

With[{n = 2},
  For[i = 0, i < 10, i++,
    a = 0;
    For[j = 0, j < n, j++,
      b = 0;
      d = RandomInteger[127];
      Print[d];
      While[d != i,
        b = b + 1;
        c = RandomInteger[];
        d = Mod[d*2, 256] + c];
      a = a + b;
      Print[Row[{i, "  ", N[a/n]}]]]]]

Please note the { ... } denotes a list of objects. It is not use to delimited code blocks. That is why I remove all the curly braces from your code.

You might find it useful to read this question and its answers.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257