Random Stepper Motion and counting steps?

Newbie question.

I’m trying to get a stepper motor to move clockwise a random number of steps between a range I’ve defined (which I can do), but then once its completed this random movement, return back to a zero state.

I thought of using a contact switch to solve the problem but I’d be really interested to learn of a coding solution. Do I need some sort of counter to work in conjunction with the random figure?

you just make the same amount of steps back to the original position?
if you just made x (which in your program is a random number between a and b) steps CW, then to get back to the original position you just have to make another x steps CCW.

Yes, keep a counter that matches the number of steps made. Update it for every move
made. If you use AccelStepper library it already keeps a counter for you,

void return_to_origin ()
{
  my_step (-stepper.currentPosition ()) ;
}

If you use the Stepper library you'll have to keep track yourself:

long position = 0L ;

void my_step (int stepcount)
{
  stepper.step (stepcount) ;
  position += stepcount ;
}

void return_to_origin ()
{
  my_step (-position) ;
}