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

Add random function for gcode macros. #5219

Merged
merged 4 commits into from
Dec 9, 2020

Conversation

ardenpm
Copy link
Contributor

@ardenpm ardenpm commented Nov 22, 2020

Adds a random(min,max) function for gcode macros. Fixes #4001

Update to upstream master.
Update to upstream master.
… number in the range specified by the parameters. The type of parameters defines the type of the output, if the inputs are doubles then result will be a double and if the inputs are int the output will be in. Uses C++11 random functionality, compiler compatibility on Linux should be checked. Fixes prusa3d#4001.
bubnikv added a commit that referenced this pull request Dec 9, 2020
The thread local storage variables for the random number generator
were replaced by an external context variable.
Thread local storage is a scarce resource.
@bubnikv bubnikv merged commit 1e190af into prusa3d:master Dec 9, 2020
@bubnikv
Copy link
Collaborator

bubnikv commented Dec 9, 2020

Merged, reworked with 7353d8c
The thread local storage variables for the random number generator
were replaced by an external context variable.
Thread local storage is a scarce resource.

@smirgol
Copy link

smirgol commented Mar 9, 2021

I might be wrong but to me it looks like the rng is not initialized with a random seed before rolling for a random number, thus always producing the same random numbers.

Like put this in the Start G-code:
G1 X{random(1.0,3.0)} Y10 F3000 ; test
It will always produce this:
G1 X1.27095 Y10 F3000 ; test

Where X should be random and not always 1.27095

@ardenpm
Copy link
Contributor Author

ardenpm commented Mar 9, 2021

I might be wrong but to me it looks like the rng is not initialized with a random seed before rolling for a random number, thus always producing the same random numbers.

What platform are you running on? It may well depend on the implementation being used on your device by std::random_device which is permitted to use a pseudo-random number generator if a non-deterministic source is not available (see here.)

@smirgol
Copy link

smirgol commented Mar 9, 2021

I'm on linux and a friend of mine is on windows. We both get the same number.
std::random_device is not called in the affected code as far as I can see. I can find a reference to it in the SLA and the tests code, but that's not affecting the ones like MK3S as far as I understand it.

@ardenpm
Copy link
Contributor Author

ardenpm commented Mar 9, 2021

Looks like that might be part of the change @bubnikv made to remove my thread local storage.

@smirgol
Copy link

smirgol commented Mar 9, 2021

Seems so. I see that in your initial code you've seeded the generator, but that code got removed. :)

bubnikv added a commit that referenced this pull request Mar 10, 2021
Initializing random generator in PlacholderParser from system clock.
@bubnikv
Copy link
Collaborator

bubnikv commented Mar 10, 2021

It should be fixed for the G-code generator with 6820921
Not on the main branch yet (I want to save myself some branch switching / merging for such a tiny change), but eventually it will end up there).

@smirgol
Copy link

smirgol commented Mar 10, 2021

Nice, thank you very much!

bubnikv added a commit that referenced this pull request Mar 15, 2021
Initializing random generator in PlacholderParser from system clock.
@HB-Stratos
Copy link

I wanted to use this new feature in the file name, so when exporting the same model twice while testing settings I would not have to always overwrite the old gcode file in the same folder. However I get the error that the random number generator is not available in the filename processing context. I see no practical reason why that limitation should exist, so, can this be changed?

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.

Feature request: random function for gcode macro language
4 participants