Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Random function?
Johan_8
Member, Developers Posts: 3 ✭
I would like to use a randomized function with featurescript but i can't find a RAND command or similar. Any ideas?
0
Best Answer
-
ilya_baran Onshape Employees, Developers, HDM Posts: 1,211FS does not support true randomness because the feature would regenerate differently every time (see https://cad.onshape.com/FsDoc/intro.html#language-fundamentals). You can use a pseudo-random number generator (e.g. https://forum.onshape.com/discussion/comment/18945 for a relatively weak one) and you can use the feature id as a seed for more randomness.Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc7
Answers
Ilya is right, things would regenerate every time. In manufacturing and engineering that is insanely bad if every item you refresh your browser or more than one person logs into the same document your model just goes crazy. It wouldn't be wise to invent "The Onshape uncertainty principle", where measuring the model changes the result.
Onshape is not a general software for simulating random events. It is a CAD package that should hold absolute positions of all points and faces. It is hard enough to debug a model that had a small unexpected change, could you imagine the stress of the support staff, or yourself when trying to debug someone's broken model, just to find out some armature designer misused an RNG feature script?
Randomness should be calculated externally then the results then imported through a CSV or by pasting the result into a variable. That would be the proper way to handle randomness in a system like Onshape.
I can see why it's niche enough to not add the the core FeatureScript function library, but FeatureScript is flexible enough that you can make your own library of functions and include it anywhere you like. I used the pseudo-random generator for my Attractor Pattern feature to randomly distribute points on surfaces, if you wanna take a look. I agree that I wouldn't want any true randomness because whatever I do needs to be deterministic and repeatable.
https://cad.onshape.com/documents/ca03eaf542826bb98f6cc90e/w/dda165f92e2ad786bd53d97a/e/417d0cf21d506b74a988ad79.
If we allow that randomness is useful, then why not support it? And if it's not useful, then how can there be a proper way to handle it?
Wouldn't it be much better to have a pRNG with an easily defined seed so that the user has full control? This seems to me to give the identical results but be much more user friendly than asking users to generate a CSV and import it.
@Evan_Reese, the problem with the "roll your own solution" is that it guarantees fragmentation over time. Generally we don't want everyone doing their own thing, because the outcome is never as good as having one robust tool which is known to be safe and reliable. I simply copy-pasta'ed @ilya_baran's code into my model and I'm perfectly satisfied with the solution, but what if he discovers a bug and fixes it? There's no route by which I will inherit that goodness.
I agree with the issues you're describing with everyone "rolling their own solution". It definitely can get messy, and lead to people duplicating effort. It could be slightly mitigated by just importing the code from a publicly available library of FeatureScript utilities instead of copy/pasting it, that way the community can contribute to bug-fixes that will still reach everyone, but it's still not as airtight or convenient at native implementation. I don't however think that "does anyone find it useful?" is a workable heuristic for deciding what should be included natively in the program. The the error at that end of the spectrum is software bloat and a steeper learning curve. The question the devs have to constantly wrestle with is "do enough people find this useful that it's worth more complexity?" which is a much more difficult and subjective question to answer. I'm glad I don't have the pressure of making that call on behalf of all Onshape users. If I had to make the call in this one instance, I'd slightly favor not adding it, but that's just me and I think a solid case could also be made that it ought to be added, and you might be just the person to make it.
A second-class lib would be good, especially if it had official support. For instance, Particle does this where their community manager, rickkas7, is an absolute genius of a programmer. He maintains libraries which aren't (yet) appropriate for universal distribution, but they are extremely helpful for anyone who is straying up to the product's boundaries.
We are building parts and tooling that needs to be accurate to the Nth decimal place and is absolutely the same no matter whet you open it.
If Onshape were to add a 'regen' button next to the save button which triggers the rng would that be a compromise we can find common ground on?
The idea is that you get something which looks random to the untrained eye, which for our purposes is all that we need. But that random thing is actually not truly random, so it's trivial to recreate.
I definitely buy the argument that anyone looking for a pseudo-RNG should really have their head on straight concerning randomness in a system which strives hard to be utterly deterministic.
I didn't include the PRNG from the forum post in std because it's a very poor quality PRNG. When I (or someone on my team or the modeling team or an intern) catch a break from higher-priority stuff, we'll expose a mt19937 implementation to FeatureScript. True randomness (not deterministic) is of course out of the question.