Skip to main content
code formatting
Source Link
user13892
  • 9.8k
  • 2
  • 15
  • 44

To localize r, I would use Module. Otherwise, the only changes needed are inserting a missing semicolon and getting rid of the unnecessary Return calls.

ClearAll[randomHop];
randomHop[{x_, y_}] :==Module[{
  Module[{  r 
},
    r = RandomInteger[r=RandomInteger[{1, 2}];
    If[r == 1If[r==1,
        {{0.5, 0.5}, {0.5, 0.5}}.{x, y},
        {{-0.5, -0.5}, {0.5, -0.5}}.{x, y} + {1, 0}]]
    ]
];

SeedRandom[1];  

NestList[randomHop, {1, 2}, 10]10];
{{1, 2}, {-0.5, -0.5}, {1.5, 0.}, {0.75, 0.75}, {0.25, 0.}, {0.125, 0.125}, 
 {0.125, 0.125}, {0.125, 0.125}, {0.875, 0.}, {0.4375, 0.4375}, {0.5625, 0.}}

Visualize the path like:

NestList[randomHop,{1,2},10]//ListLinePlot

To localize r, I would use Module. Otherwise, the only changes needed are inserting a missing semicolon and getting rid of the unnecessary Return calls.

randomHop[{x_, y_}] :=
  Module[{r},
    r = RandomInteger[{1, 2}];
    If[r == 1,
      {{0.5, 0.5}, {0.5, 0.5}}.{x, y},
      {{-0.5, -0.5}, {0.5, -0.5}}.{x, y} + {1, 0}]]

SeedRandom[1]; NestList[randomHop, {1, 2}, 10]
{{1, 2}, {-0.5, -0.5}, {1.5, 0.}, {0.75, 0.75}, {0.25, 0.}, {0.125, 0.125}, 
 {0.125, 0.125}, {0.125, 0.125}, {0.875, 0.}, {0.4375, 0.4375}, {0.5625, 0.}}

To localize r, I would use Module. Otherwise, the only changes needed are inserting a missing semicolon and getting rid of the unnecessary Return calls.

ClearAll[randomHop];
randomHop[{x_,y_}]:=Module[{
    r 
},
    r=RandomInteger[{1,2}];
    If[r==1,
        {{0.5,0.5},{0.5,0.5}}.{x,y},
        {{-0.5,-0.5},{0.5,-0.5}}.{x,y}+{1,0}
    ]
];

SeedRandom[1]; 

NestList[randomHop,{1,2},10];
{{1, 2}, {-0.5, -0.5}, {1.5, 0.}, {0.75, 0.75}, {0.25, 0.}, {0.125, 0.125}, 
 {0.125, 0.125}, {0.125, 0.125}, {0.875, 0.}, {0.4375, 0.4375}, {0.5625, 0.}}

Visualize the path like:

NestList[randomHop,{1,2},10]//ListLinePlot
edited body
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

To localize fr, I would use Module. Otherwise, the only changes needed are inserting a missing semicolon and getting rid of the unnecessary Return calls.

randomHop[{x_, y_}] :=
  Module[{r},
    r = RandomInteger[{1, 2}];
    If[r == 1,
      {{0.5, 0.5}, {0.5, 0.5}}.{x, y},
      {{-0.5, -0.5}, {0.5, -0.5}}.{x, y} + {1, 0}]]

SeedRandom[1]; NestList[randomHop, {1, 2}, 10]
{{1, 2}, {-0.5, -0.5}, {1.5, 0.}, {0.75, 0.75}, {0.25, 0.}, {0.125, 0.125}, 
 {0.125, 0.125}, {0.125, 0.125}, {0.875, 0.}, {0.4375, 0.4375}, {0.5625, 0.}}

To localize f, I would use Module. Otherwise, the only changes needed are inserting a missing semicolon and getting rid of the unnecessary Return calls.

randomHop[{x_, y_}] :=
  Module[{r},
    r = RandomInteger[{1, 2}];
    If[r == 1,
      {{0.5, 0.5}, {0.5, 0.5}}.{x, y},
      {{-0.5, -0.5}, {0.5, -0.5}}.{x, y} + {1, 0}]]

SeedRandom[1]; NestList[randomHop, {1, 2}, 10]
{{1, 2}, {-0.5, -0.5}, {1.5, 0.}, {0.75, 0.75}, {0.25, 0.}, {0.125, 0.125}, 
 {0.125, 0.125}, {0.125, 0.125}, {0.875, 0.}, {0.4375, 0.4375}, {0.5625, 0.}}

To localize r, I would use Module. Otherwise, the only changes needed are inserting a missing semicolon and getting rid of the unnecessary Return calls.

randomHop[{x_, y_}] :=
  Module[{r},
    r = RandomInteger[{1, 2}];
    If[r == 1,
      {{0.5, 0.5}, {0.5, 0.5}}.{x, y},
      {{-0.5, -0.5}, {0.5, -0.5}}.{x, y} + {1, 0}]]

SeedRandom[1]; NestList[randomHop, {1, 2}, 10]
{{1, 2}, {-0.5, -0.5}, {1.5, 0.}, {0.75, 0.75}, {0.25, 0.}, {0.125, 0.125}, 
 {0.125, 0.125}, {0.125, 0.125}, {0.875, 0.}, {0.4375, 0.4375}, {0.5625, 0.}}
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

To localize f, I would use Module. Otherwise, the only changes needed are inserting a missing semicolon and getting rid of the unnecessary Return calls.

randomHop[{x_, y_}] :=
  Module[{r},
    r = RandomInteger[{1, 2}];
    If[r == 1,
      {{0.5, 0.5}, {0.5, 0.5}}.{x, y},
      {{-0.5, -0.5}, {0.5, -0.5}}.{x, y} + {1, 0}]]

SeedRandom[1]; NestList[randomHop, {1, 2}, 10]
{{1, 2}, {-0.5, -0.5}, {1.5, 0.}, {0.75, 0.75}, {0.25, 0.}, {0.125, 0.125}, 
 {0.125, 0.125}, {0.125, 0.125}, {0.875, 0.}, {0.4375, 0.4375}, {0.5625, 0.}}