Skip to content

MNT: do not use srand unless we have to - #5725

Closed
tacaswell wants to merge 1 commit into
matplotlib:v1.5.xfrom
tacaswell:fix_reset_srand
Closed

tacaswell wants to merge 1 commit into
matplotlib:v1.5.xfrom
tacaswell:fix_reset_srand

Conversation

@tacaswell

Copy link
Copy Markdown
Member

Resetting the random number generator to seed at 0 is done as part
of rewinding the Sketch path convertor. However, this causes
the seed to be reset on every call to draw if Agg is being used which
interferes with users how are using rand in their own code and
embedding python [1].

This moves the srand call inside the conditional that deals with paths
that really are sketched so you only get disrupted if you are really
using this feature.

1 http://stackoverflow.com/q/34422244/380231

@mdboom

Resetting the random number generator to seed at 0 is done as part
of rewinding the `Sketch` path convertor.  However, this causes
the seed to be reset on every call to `draw` if Agg is being used which
interferes with users how are using `rand` in their own code and
embedding python [1].

This moves the `srand` call inside the conditional that deals with paths
that really are sketched so you only get disrupted if you are really
using this feature.

1 http://stackoverflow.com/q/34422244/380231
@tacaswell tacaswell added this to the Critical bugfix release (1.5.1) milestone Dec 23, 2015
@mdboom

mdboom commented Dec 23, 2015

Copy link
Copy Markdown
Member

I don't know if that's a system call of just a C library call, but this should be a modest speed improvement as well. As alternative solution might be to not use the C random number calls (which use global state), to using one of the more modern C++ implementations where the object could manage its own state. Or, honestly, if you don't care about crypto (which we don't here) we could just write our own random number generator in a few lines.

@tacaswell tacaswell closed this Dec 23, 2015
@tacaswell
tacaswell deleted the fix_reset_srand branch May 16, 2016 23:26
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

Successfully merging this pull request may close these issues.

2 participants