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

Semantics of randSetSeed and randIntU #761

Open
dlunde opened this issue Jul 6, 2023 · 3 comments
Open

Semantics of randSetSeed and randIntU #761

dlunde opened this issue Jul 6, 2023 · 3 comments

Comments

@dlunde
Copy link
Contributor

dlunde commented Jul 6, 2023

Currently, the intrinsics randSetSeed and randIntU have a somewhat peculiar semantics. Specifically, the first occurrence of randIntU randomly initializes the seed of the pseudo-random number generator (Random.self_init in boot), unless you call randSetSeed before this first occurence. This caused some unexpected behavior in combination with externalSetSeed that, in addition to setting the standard OCaml Random library seed, also initializes random number generators for Owl. For example, the call to randIntU below randomly initializes the seed even though we call externalSetSeed.

externalSetSeed 0 -- Calls Random.init 0 internally
let x = randIntU 0 10 in -- Calls Random.self_init internally
...

I fixed this for my use cases by constructing a function setSeed that internally calls both externalSetSeed and randSetSeed:

setSeed 0 -- Calls externalSetSeed and randSetSeed internally
let x = randIntU 0 10 in -- Does _not_ call Random.self_init internally
...

However, I think that we should change the current randSetSeed and randIntU semantics to one of the following:

  1. Simply do not call Random.self_init in randIntU. The result is that the seed is set to a standard value in each execution. If we want random seed initialization, we can add another intrinsic randSeedSelfInit or similar.
  2. Call Random.self_init once, in the very beginning of the program, and not in conjunction with randIntU.
@lingmar
Copy link
Contributor

lingmar commented Aug 1, 2023

Maybe we could remove randSetSeed and randIntU as intrinsics since the same functionality is implemented in externals?

@dlunde
Copy link
Contributor Author

dlunde commented Aug 2, 2023

Yes, that would probably be the best long-term solution. I think there are some limitations with externals currently? For example, they don't work in boot (but maybe that's fine).

@lingmar
Copy link
Contributor

lingmar commented Aug 3, 2023

It might be worth checking if the uses of randIntU in the stdlib can be replaced by externals, then. If they're not part of the first pass of bootstrapping it should work, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants