0
$\begingroup$

I'd like to make a program such as:

A=1
B=-1
RandomInteger[{0,1},200];

If[#==0,
A+=Abs[B];B,
Return[A] then Re-initialize ?]&
@/RandomInteger[{0,1},200]

My problem is that A keeps incrementing. And do not re-initialize when it's called.

I'd like to print B and do A+=Abs[B] if 0; to print A then set A=1 if 1 ...

I tried to nest this loop in a meta If[] with a glance on the previous value (%), but it didn't work.

I can't use For and While neither because I need the output to be a list.

$\endgroup$

closed as off-topic by MarcoB, Yves Klett, user9660, RunnyKine, m_goldberg Mar 17 '16 at 19:16

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." – MarcoB, Yves Klett, Community, RunnyKine, m_goldberg
If this question can be reworded to fit the rules in the help center, please edit the question.

2
$\begingroup$
a = 1;
b = -1;
r = RandomInteger[1, 20]
If[# == 0,
   a += Abs[b];
   b,
   c = a;
   a = 1;
   c] & /@ r
$\endgroup$
  • $\begingroup$ Thank you so much Chris Degnen ! That's exactly what I wanted. I felt like an additional variable could do the trick. Wish you the best ! Sorry, I can't Upvote. $\endgroup$ – K-Pia Mar 17 '16 at 14:39
  • 1
    $\begingroup$ @K-Pia ... But you can accept the answer, if it solves your problem. Just click on the grey check mark next to the answer you want to accept. $\endgroup$ – MarcoB Mar 17 '16 at 14:43

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