inkandswitch / livebook

IPython notebook-compatible live coding experiment
202 stars 11 forks source link

Deterministic random number seed #50

Closed adamwiggins closed 8 years ago

adamwiggins commented 8 years ago

Because of caching, forking the Monte Carlo notebook means you see a flash as the scatter plot updates once the python runtime is loaded.

How about setting a deterministic seed (maybe from the document URL) so that random calls always return the same value for that notebook?

luciasantamaria commented 8 years ago

Good idea. There needs to be a call to random.seed('URL') before sample_widths = [random.random() * wall_width for i in range(number_of_marbles)]

Not sure how to pass the URL value, generated at the time of forking.

adamwiggins commented 8 years ago

Ah good idea, do it in userspace. You could modify the sample notebook to include random.seed(123454321) or something near the top.

But I was thinking of changing Livebook to always seed with a fixed value for the URL. Maybe that's hard to do if random has not been imported by the user's code?

luciasantamaria commented 8 years ago

We'll go with random.seed(123454321) in userspace if nothing else works. But I'd rather use the URL value, which I somehow thought could be passed to the python code. Maybe something a bit hacky: random.seed('URL') where 'URL' is a variable set by Livebook itself. After forking, 'URL' is created as local variable, then the python code is loaded (including the package random), then random.seed('URL') should work (?).

adamwiggins commented 8 years ago

It's a good idea, but I'm leery of Livebook special variables. e.g. what would the behavior be if you saved/downloaded the .ipynb and loaded it in Jupyter? Having the system seed for you (which seems to me always a good idea) doesn't break any Jupyter compatibility.

luciasantamaria commented 8 years ago

@orionz I'd like to quietly call random.seed(URL) "behind the scenes". Is this possible? Where to put this code? Needs to run after 'import random'.

orionz commented 8 years ago

commit ece6b53affe9e9a8b2748ecbdf6b9f4a02350c4b

can you test?

adamwiggins commented 8 years ago

Confirmed improvement: live coding does not cause the scatter plots to move around (unless you change one of the parameters). So seeding is working.

There are still two "flashing" problems: (1) updates cause the scatter plot to fade out and back in even briefly even though the points are in the same position, and (2) the cached results in the notebook don't match the live results, so there's a flash after the Python runtime has finished initializing.

Should I open separate tickets for those?

luciasantamaria commented 8 years ago

Yes please, two new issues.