Just getting back into Twine and the newer version of Harlowe for some workshops I’m preparing - ran into strange behaviour.
Working in the browser-based editor - if I make a simple die roll with (random:) or display a string with (choose:) then it always seems to display the same value, even if I refresh over and over. The weird thing is that if I view the same page using the Debug button, instead of Play, then refreshes seem to change up the value as I would expect.
Is there something I’m missing or doing wrong here?
I believe Harlowe prefers that randomness is repeatable (perhaps for testing purposes). Hitting refresh in the browser produces the same random results over and over again.
What you want is to alter the seed used to generate random numbers, but the seed needs to change for each session so you can use the date/time stamp on the user’s computer.
I’m feeling rusty with Harlowe so I used JavaScript to set a Harlowe variable based on the number of milliseconds since January 1, 1970. I don’t believe Harlowe has a similar feature, is why.
If you place that code in a passage and hit refresh it should change the random number each time.
Remember that the seed value must be a string and it should only have to be set once each time the game is started, loaded, or refreshed. You could do it in a startup passage, if you want.
Edit: To put this seed mechanic into further context, MineCraft allows you to generate a random world to explore, but you can share the seed used with your friends and their copies of Minecraft will generate the exact same world. A computer cannot generate a random number out of thin air. It needs a seed.
But humans can pick numbers randomly, right? Well… an Oracle once said, “Ohh, what’s really going to bake your noodle later on is, would you still have broken it if I hadn’t said anything?”
As explained by HAL9000, and in parts of Harlowe’s own (seed:) macro documentation, Harlowe 3.x uses Pseudo Random Number generation (aka PRNG). Unlike some other Story Formats, Harlowe doesn’t include a non-PRNG based option for random number generation.
When a Harlowe based Story HTML file is opened in a new web-browser tab, the story format’s runtime engine automatically generates a new seed, which is then used by the story format’s “random” family of macros.
note: The (seed:) macro can be used by an Author to replace the automatically generated seed with one of their choosing.
The current seed (and its associated iteration index) is stored in Progress History, the current Moment of which is automatically persisted to the sessionStorage cache the web-browser created & allocated to this tab.
Whenever a page refresh occurs Harlowe automatically reloads the current Moment from sessionStorage, which results in the current seed (and its associated iteration index) being restored. Which is why the “next” random number generated doesn’t “change”.
So if a Harlowe based Story HTML file is opened in a different web-browser tab it will have a different automatically generated seed, because that tab has its own new sessionStorage cache.
Thank you both so much for the insightful and detailed answers! This explains what I was experiencing so clearly and also gives me plenty of context to explore it later if I need to manipulate the seed.