0
$\begingroup$
Part1
s = {0., 0.};
path = Table[s += RandomReal[{-0.5, 0.5}, 2], {100}];
ListLinePlot[path, ImageSize -> Small]
path

This part of the code generates 100 xy coordinates or steps and I need a code to calculate total distance with a DO loop

$\endgroup$
  • $\begingroup$ Welcome to MSE. Is this a homework question? If so, please add the "homework" tag and update your post with the ideas and/or code you have used to try and solve the problem. $\endgroup$ – Rohit Namjoshi Feb 7 at 2:21
5
$\begingroup$
SeedRandom[1]
s = 0; path = Table[s += RandomReal[{-0.5, 0.5}, 2], {100}];

You can use ArcLength or RegionMeasure:

ArcLength[Line@path]

36.96705

RegionMeasure[Line@path]

36.96705

| improve this answer | |
$\endgroup$
2
$\begingroup$

With

path = Accumulate@Prepend[RandomReal[{-0.5, 0.5}, {100, 2}], {0, 0}];

here are a couple of possibilities:

EuclideanDistance @@@ Partition[path, 2, 1] // Total
BlockMap[EuclideanDistance @@ # &, path, 2, 1] // Total
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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