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