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$
1
  • $\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$ Commented Feb 7, 2020 at 2:21

2 Answers 2

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

$\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
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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