Jump to content

Create your own Generic VI (like Randomize 1D Array)


Recommended Posts

WARNING! As Aristos Queue stated, this is VERY experimental. I am not an employee of NI and am not responsible for anything bad that happens if you use this. Basically, it's just for experimentation.

4mgL4.png

Just drop that VI snippet into a VI, and it will work with any array type. I copied it from Randomize 1D Array. If you don't know what a generic VI is, click that link.

Here's a sample I created that takes a 1D array of any type, and outputs an array which is basically that array concatenated to itself. For example, {1, 2, 3} becomes {1, 2, 3, 1, 2, 3}. Not too useful, but it works as a proof-of-concept. And in case the VI snippet isn't working, you can copy the control and indicator from there.

Download link: Duplicate 1D Array.vi

Another thing that might work (though I haven't tried it) is to take the DBL out of the array and see if that works with any type as well (not just any array). I can still think of a few uses for this, such as if the value is just going to be typecasted anyway. Like, say, something that converts any data into a hex string.

EDIT: Yep, it works, and here you go. Data to Hex String.vi

EDIT2: There is actually a much easier way to do this, which is likely how Aristos made the generic controls in the first place. All you have to do is open LabVIEW.ini, add the line "GenericsAreGo=True" somewhere, and you'll be able to right-click a control and select "Generic" to make it generic.

EDIT3: Out of curiosity, I tried putting a generic control in a Malleable VI to see what would happen, since Malleable VI's basically do the same thing. Guess what? Crash. Not immediate, but it happened after I moved the subVI on the diagram. Fix your code, NI! (just kidding!)

Edited by flarn2006
Files moved from external download site to LAVA.
Link to comment

One thing you might wish to look up is Norm's scripting springboard. It's a template VI which has code to operate on a selection in a VI when you press a button. That means you can create a copy of the template, add your code to it and easily test that it works and use it. It's certainly easier than having to drop controls from the palette.

And again, to expand on AQ's warning, he didn't just say it was experimental. He basically said the experiment is over and that this tech would not be developed further.

Link to comment

As a general warning to anyone interested, I guarantee you'll be able to crash LabVIEW with these, so you're nuts if you even think about using this stuff in production code. A few issues I know of:

1) The compiler gets confused when you stack them deeply as subVIs. Not sure why.

2) If you copy your VIs to a new machine where they've never been loaded before, your load time goes through the roof for first load, so much so that most people might believe LV is hung and kill it.

3) These VIs do not play nicely with recursion.

It's a very cool toy, but like Yair mentioned the experiment is essentially over. I'd love to see a stable replacement for this kind of concept (think of the possibilities for reuse...).

Link to comment

One thing I'm still curious about is how NI makes generic VI's. Obviously the controls have to come from somewhere, so how do you make them? Is it like some kind of low-level VI editor that edits the individual data structures in the file? Also, how exactly does the compiler get confused?

Edited by flarn2006
Link to comment

One thing I'm still curious about is how NI makes generic VI's. Obviously the controls have to come from somewhere, so how do you make them? Is it like some kind of low-level VI editor that edits the individual data structures in the file? Also, how exactly does the compiler get confused?

Well how do you think did they do the first controls of a new data type? Probably something like handcoding with specially compiled LabVIEW that has special tools included. And about how the compiler gets confused, I'm sure you will never hear a detailed info. For one thing this is NI internal, and for another thing unless you understand a project like LLVM from ground up, it would make no sense to you if they would give you a more technical explanation. Go study LLVM and once you understand it, you may be qualified to understand at least in parts what all might go wrong there.

Link to comment

Well how do you think did they do the first controls of a new data type? Probably something like handcoding with specially compiled LabVIEW that has special tools included. And about how the compiler gets confused, I'm sure you will never hear a detailed info. For one thing this is NI internal, and for another thing unless you understand a project like LLVM from ground up, it would make no sense to you if they would give you a more technical explanation. Go study LLVM and once you understand it, you may be qualified to understand at least in parts what all might go wrong there.

I'd love it if someone from NI could post a few screenshots of that specially-compiled version, just out of curiosity.

Link to comment

Not a special compiled version, just a certain way we can configure the UI to make controls generic.

Could you post a screenshot of the menu or whatever else it is you use to customize that? It's not like it would give us access to any features NI doesn't want us to have, just satisfy my curiosity about that kind of thing.

Link to comment

Could you post a screenshot of the menu or whatever else it is you use to customize that? It's not like it would give us access to any features NI doesn't want us to have, just satisfy my curiosity about that kind of thing.

No, I can't post a screenshot. Your curiosity will have to be satisfied with "a certain way we can configure the UI to make controls generic". :)

  • Like 1
Link to comment

Aristos Queue mentioned to me that Generic VI's will no longer work in LV2012. :( It's a shame they couldn't have changed it so it was more stable, but I guess since it's unreleased they don't really have any obligation to.

But as consolation (actually just for fun), here's another Generic VI I made that takes a random choice from an array.

Random Choice from Array.vi

OT1YV.png

EDIT: Actually that has a glitch where it will sometimes give the default value. This is no fault of the generic VI system; I simply forgot to add a Dec1func.png on the blue wire between the ytRsJ.png and the IndexArray.png. But when I tried it afterwards, LabVIEW crashed, which shouldn't come as much of a surprise. But it's a simple enough fix.

Edited by crelf
Files moved from external download site to LAVA.
Link to comment

EDIT: Actually that has a glitch where it will sometimes give the default value. This is no fault of the generic VI system; I simply forgot to add a Dec1func.png on the blue wire between the ytRsJ.png and the IndexArray.png. But when I tried it afterwards, LabVIEW crashed, which shouldn't come as much of a surprise. But it's a simple enough fix.

I think your proposed fix is not a fix but just a change of the bug. With the fix as proposed by you you end up with an index in the range -1 to n-1 instead of 0 to n. So you replace the invalid index at the end of the range with one before the range. The -1 should be better placed after the Array Size function.

  • Like 1
Link to comment

I think your proposed fix is not a fix but just a change of the bug. With the fix as proposed by you you end up with an index in the range -1 to n-1 instead of 0 to n. So you replace the invalid index at the end of the range with one before the range. The -1 should be better placed after the Array Size function.

Oh yes, that makes sense. And Index Array works the same on an empty array regardless of what index is given, right? Even a negative number? It just gives the default value for the array?

Link to comment

Oh yes, that makes sense....

But even then it is not a correct solution for choosing a random array element. At least when you want uniform distribution. Look at the histogram made for 20-element array and 1e6 choices:

post-7450-0-57485300-1341388558.png

Make a conclusion yourself.

BTW, who said it:

When you think that something is simple, it means that you don't understand the problem

?

Link to comment

Lol, this is turning into the original discussion about Randomize 1D Array. :lol:

But yeah, I didn't realize that ytRsJ.png rounded to the nearest integer. I had always thought it just rounded toward 0. I would use Rounddn.png, but I would like a universal solution that would work for negative numbers too, even though negative numbers wouldn't come up in that case. I could have it set up so it rounds up if a number is negative, or down if positive (using a case structure or the ever-useful Selfunct.png) but that seems too complicated for a task as simple as generating a random integer between two values—surely there must be a simpler way?

Link to comment

Lol, this is turning into the original discussion about Randomize 1D Array. :lol:

But yeah, I didn't realize that ytRsJ.png rounded to the nearest integer. I had always thought it just rounded toward 0. I would use Rounddn.png, but I would like a universal solution that would work for negative numbers too, even though negative numbers wouldn't come up in that case. I could have it set up so it rounds up if a number is negative, or down if positive (using a case structure or the ever-useful Selfunct.png) but that seems too complicated for a task as simple as generating a random integer between two values—surely there must be a simpler way?

Mathematics is tricky. The round to zero is possible with the floor() operation but is not sufficient. Since Randomize() produces values between 0 and 1 with the ends inclusive, you still can end up with a few potential values outside the range every now and then.

Also just as a trivia there are in fact two round() used in mathematics, one rounds 0.5 always up the other towards even numbers. The second is meant to reduce the effect of rounding errors in combined mathematic calculations with multiple roundings occuring.

Link to comment

... Since Randomize() produces values between 0 and 1 with the ends inclusive, you still can end up with a few potential values outside the range every now and then.

To be precise:

0 <= "Random Number (0-1)" < 1

So rounding towards -Inf is a valid approach

Link to comment

No, I can't post a screenshot. Your curiosity will have to be satisfied with "a certain way we can configure the UI to make controls generic". :)


Is this functionality in the release version of LabVIEW, just not enabled? I mean, like is there any way a hacker could configure the UI like this just by modifying a few bytes or something?

 

EDIT: Yes it is! LabVIEW.ini, "GenericsAreGo=True"

Edited by flarn2006
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.