PDLPorters/pdl

srand and random (randsym) weird issue

Closed this issue · 6 comments

This is on PDL 2.086

From within a perl script:
say srand(5); say PDL->random(5); say srand(5); say PDL->random(5);
5
[0.780595644307593 0.959459289116744 0.869456507986439 0.416370155335506 0.303093156409]
5
[0.753452489726679 0.473468645621144 0.332829359005402 0.330570439363902 0.78674208901834]

From within the pdl environment

pdl> srand(5)
pdl> p random(5)

[0.486107457310537 0.0505676127741495 0.0577106056898107 0.655357255937979 0.54565243776489]

pdl> srand(5)
pdl> p random(5)

[0.486107457310537 0.0505676127741495 0.0577106056898107 0.655357255937979 0.54565243776489]

So srand does not seem to work as expected when called from within perl. This limits the reproducibility of random streams produced. On the other hand PDL demonstrates the expected behavior. Am I missing something?

Possibly related to this behavior: while calling srand from within perl as:
srand(123);

works, calling it as:
PDL->srand(123);

generates an error message:

called object method on 'PDL' or similar at primitive.pd line 2186.
PDL::srand("PDL", 123) called at ./simGSL.pl line 86

As noted by haj on IRC, this is down to what your Perl script is importing into its namespace. PDL::Primitive exports srand, which perldl can see. I assume your script does something like use PDL::Lite.

For this sort of thing, you really need to show the whole piece of code, so that the poor sap who tries to debug it can copy-paste it into their environment to reproduce it. Saying "within a perl script" isn't really a substitute for that.

Apologies for the confusion and my drama. Perhaps make a statement about this not being a class method in the documentation? The code snippet looks like this

srand(); # uses current time srand(5); # fixed number e.g. for testing

perhaps add something like

When calling from within a perl application:

PDL::srand(); # uses current time PDL:: srand(5); # fixed number e.g. for testing

Feel like making a pull request to update docs?

I to create one as you were suggesting it, but it does not let me move forward with the pull request.

Have you looked in the FAQ?