The "History Backward" / Undo button moves the story's History system backwards one Moment in time to **before** the previous Passage was processed, and then re-processes that previous Passage again.
This is why the <<set>> macro in "passage 1" is executed again, and why $random can end up with a different value.
warning: Doing this will make any old saves that you (or the Readers of your story) now invalid, and you will either need to:
a. [simple] Delete those old saves.
b. [advanced] Use a Config.saves.onLoad handler combined with the Config.saves.version property to patch the old saves as they are being loaded.