Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import of nimib breaks expected behaviour of std / random #204

Closed
pietroppeter opened this issue Apr 1, 2023 · 3 comments · Fixed by #211
Closed

import of nimib breaks expected behaviour of std / random #204

pietroppeter opened this issue Apr 1, 2023 · 3 comments · Fixed by #211
Labels
bug Something isn't working

Comments

@pietroppeter
Copy link
Owner

expected behaviour of std / random is that the following should output always the same result, which it does not unless I remove nimib import:

import std / random
import nimib # if I comment this lines results are reproducible
echo rand(100)

this is a side effect of the dependency on tempfile, see: OpenSystemsLab/tempfile.nim#13

this is particular bad in nimib since tempfile is called every time a new nbCode is created (a temporary file is created to save the output) and this seems to trigger a new call to randomize. This means that a single randomize(42) call after importing nimib is not enough to guarantee reproducible results, the user must call randomize with a fixed seed inside every code block that calls random functions.

import nimib
import std / random
randomize(42) # putting a randomize here does not guarantee reproducible results
nbInit
nbCode:
  randomize(42) # putting randomize here does!
  echo rand(100)
echo nb.blk.output

An alternative to tempfile are some utilities in std lib but they do not yet support the same functionalities of tempfile: https://nim-lang.org/docs/tempfiles.html

@pietroppeter pietroppeter added the bug Something isn't working label Apr 1, 2023
@pietroppeter
Copy link
Owner Author

this is where nimib uses tempfile:

(tmpFile, tmpFilename) = mkstemp(mode=fmWrite)

it is not completely clear to me why randomize seems to be called every time we capture output...

@pietroppeter
Copy link
Owner Author

btw the file that we use for capturing output could be something with a more predictable name and we could probably reuse the same file every time (we would need to change captureStdout api)

@pietroppeter
Copy link
Owner Author

Indeed they say tempfile is deprecated and we should switch to tempfiles from stdlib (see issue referenced above)

HugoGranstrom pushed a commit that referenced this issue Aug 10, 2023
* Fix std/random bug

- Remove OpenSystemsLab/tempfile.nim and replace with build-in `std/tempfiles`
- Remove low level stuff that might break in newer Windows update and replace with build-in `fusion/ioutils`
- Add ability to name the temp file

* Reorder template and change from `when` to `if`

* Update nimib.nimble

* Remove filename overload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant