-
Notifications
You must be signed in to change notification settings - Fork 26
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
Adding Random to standard libraries #475
Conversation
Adds a new passive class "Random" to the standard libraries. Each instance of Random encapsulates a seed of its own. Technically, this may be wasteful -- possibly one seed per thread might do, but this will facilitate debugging programs with random numbers.
@EliasC tells me there is no place for standard library documentation. Therefore, I proclaim this ready to be merged. |
@TobiasWrigstad It still needs to be reviewed. No skipping steps. |
-- Initialisation always with a seed | ||
def init(seed:int) : void | ||
embed void | ||
_this->_enc__field_seed = (unsigned) #{seed}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of relying on _this->... = ...
, is it possible to write this.seed = embed ... end
. we avoid relying on the internal name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Internal names are going to change real soon, so relying on them is a bad idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thing is that I am storing an unsigned
inside of an int64_t
. Therefore I preferred to do it all at the C-level. What if the C code inserts a cast? Seems better to rely on local things only.
The other possibility is to have an embed type:
seed: embed unsigned end
but this will require adding an empty trace function. I could do that. But I would still need to do _this->
because "unsigned" is not a valid Encore type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you only need the cast when you are passing the seed to rand_r
?
So,
translates into
whereas
translates into
I must confess that it is somewhat unclear to me what the consequences are of casting a signed int to an unsigned into and then into a signed int again, in this scenario. Does anyone have an idea? I suspect this matters little in the end, as long as the behaviour of assigning seed N is consistent. |
@supercooldave I don't think relying on internal names is a big problem — I suspect this is the module system messing with things? This is an easy fix once the changes are in. |
@supercooldave Sorry, I meant ready to be reviewed. Precisely I mean, "I feel it can be merged to someone should review it". |
@kikofernandez Can the commits be squashed during merge, or should I do that some other way? |
@TobiasWrigstad do you experience this same issue?
|
@kikofernandez Yes. But not before. I wonder what I have changed. Could it be the |
@TobiasWrigstad we can squash the commits, you don't have to do anything |
@kikofernandez @TobiasWrigstad The problem is that you have a file called random in both the local directory and one in the standard library, so when doing the import it doesn't know which one to import -- which is obviously a bug. For now, rename the test file to |
I can't get rid of the error. This seems like something that @supercooldave should know how to fix/debug. |
Does this have something to do with the case insensitive file system on the Mac? |
@TobiasWrigstad Did you see my comment? Rename the test file to |
@supercooldave Thanks, I just saw. Test passes now! @kikofernandez I don't know what you mean. The seed is not used in the code. |
never mind, I thought that something like this may work:
|
but I don't know and need to embark now... |
https://github.com/TheGrandmother/Boids/blob/master/boids1/Random.enc We had similar attempts during the hackathon in London. |
@kikofernandez I see what you mean, but it won't work unless you write it like this:
because there is a side-effect on @EliasC Does inference work with embed types? @albertnetymk — feel free to backport things into this PR if you think we need more bells and whistles. |
@kikofernandez bump. See my rewrite of your code 4 days ago. |
@TobiasWrigstad regarding:
the thing is that we are doing the same cast in your original solution (first one above) given that the signature is:
This makes me wonder if we should check on the limits, Consequence: this is not random anymore, big numbers now match small numbers and the random generator is not secure (to use in encryption apps). I think it's better to crash at runtime |
@TobiasWrigstad regarding:
I do not like it at all... if makes it difficult to read. For this reason, I think it's better to keep your current implementation |
I think the PR is ready to merge as soon as we figure out the kind of behavior we want for the random numbers. This means, if a number is bigger than 32 bits, do we crash or do we do the cast and work on 32 bits? |
@kikofernandez Re the 2^32 check. Excellent suggestion! Thanks! |
@kikofernandez I've elected to check the seed but not the random function -- that will be too expensive in benchmarks that hit random a lot. Are you fine with that? |
@TobiasWrigstad the random function doesn't do any downcast, it's always 32-bits. So I think this is ok. |
Adds a new passive class "Random" to the standard libraries.
Each instance of Random encapsulates a seed of its own.
Technically, this may be wasteful -- possibly one seed per
thread might do, but this will facilitate debugging programs
with random numbers.
Documentation still lacking. Where should I put that?