LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
JackDunaway

Randomize Array

Status: New

A common need that comes up is the ability to randomize an array, yet there is no primitive that handles this. There are of course a few homebrew solutions, but it's tough to beat a primitive that accepts any dimension or datatype in terms of syntax and execution speed!

 

17359iAF869714800E9778

 

Note: This is not an Idea to create an array of random numbers, it only deals with taking an already initialized array and scrambling the locations of the elements.

12 Comments
AristosQueue (NI)
NI Employee (retired)

> accepts any dimension

 

The glyph is the inverse of Sort 1D Array, which only handles 1D arrays (thus its name). When shuffling higher dimensional arrays, are you intending that each row would be individually shuffled or would you want shuffling across the plane/cube/hypercube/etc?

Darin.K
Trusted Enthusiast

I would think the Knuth Shuffle as performed by the Riffle VI would be effective for this.

altenbach
Knight of NI
These two quoted solutions are rube goldberg contracption. 😉

 

 

I agree with Darin, riffle should be sufficient for most cases.

 

 

Or you can make your own, e.g. as follows.

 

 

If you want higher dimensions, frame it with two "reshape array" functions, being 1D in the middle. (see also)

JackDunaway
Trusted Enthusiast

AQ: Oh yeah, good call about the glyph, it's not a perfect analogue to the "Sort 1D Array" (my memory failed to recall '1D'). To clear up the confusion, one of two things could happen:

 

1. Come up with a new glyph with no association to the Sort 1D Array, and the function truly scrambles all elements over every dimension

2. Revise my suggestion to only be "Randomize 1D Array" and keep the glyph as a perfect counterpart to "Sort 1D Array"

 

Honestly, #2 would suffice for me, and would probably cover most people's use case. I'll leave it up to others (R&D, this thread, et al.) to discuss on which solution is more desirable.

AristosQueue (NI)
NI Employee (retired)

> I would think the Knuth Shuffle as performed by the Riffle VI would be effective for this.

 

It probably would, except that VI can't take an array of strings, array of booleans, etc.

JackDunaway
Trusted Enthusiast

@Darin.K - Excuse my ignorance, but "riffle" sounds like a stuck-up British term. 🙂 You and altenbach are truly exceptional to have known about this VI, and I can guarantee that neither I nor the masses would have known to look under "Signal Processing > Signal Operation > Riffle" when we're simply trying to write quickie VI to determine whose turn it is to pay for lunch today. Plus, that VI is merely polymorphic over a couple numeric scalar datatypes, and will not handle clusters/strings... So, Riffle... yes..... but no.

 

@Rube-killer: As gorgeous as your solution is, think of how elegant it would look with the Naked For Loop or the Index and Unbundle!

 

***Edit: Strikeout above because AQ beat me to it.***

altenbach
Knight of NI

For more complicated 1D array structures, we might not want to drag everything through the sort array node for simplicity. Here's a more generic version that works more generally. (top)

 

And if we had generic terminals (as suggested in my comment here), we could make a generic polymorphic subVI as in the bottom of the image and stop worrying. (bottom) 😉

 

Darin.K
Trusted Enthusiast

I would like to say it is my hours at the Blackjack table that introduced me to the term Riffle, but alas it was Bridge, which is a pretty stuffy game I'll admit.  Let's get it added to the LV lexicon.

 

Your picture immediately illicited a response of Riffle from me, showing a string array would have stopped that impulse.  I like altenbach's generic datatype since I randomize my general arrays as follows

 

17375i9416AA1D83F59C55

 

At the bottom I show my typical use of Riffle.  When I need the integers [0-N-1] in random order, say for indexing a generic array, I use the bottom code. 

 

Anything is better than those homebrew solutions.  I could see adding a few common datatypes to the polymorphic types supported by Riffle.  Riffling the kitchen sink, I'll let R&D decide how hard that is to implement.

 

(The number of times I have used the term Riffle in my life has probably quadrupled thanks to this thread).

altenbach
Knight of NI

> The number of times I have used the term Riffle in my life has probably quadrupled thanks to this thread.

 

Maybe we should found the National Riffle Association. 😄

 

I don't really like the term "riffle" and would have preferred something more generic such as "shuffle" or "randomize 1D array". Riffle is a special kind (in the typical definition) and maybe statistically a bit suspect, because the sort order of each half is retained for each pass. Just guessing here.

Darin.K
Trusted Enthusiast

Since I can't resist using Riffle a few more times, if there is a National Riffle Association then consider me Charleton Heston.

 

Thanks to the link I'll start calling it the Fisher-Yates shuffle, apparently my version of Knuth's book is a bit old and does not give credit where credit is due.  Where NI got Riffle I am not sure.  Riffle in Mathematica is quite different (and I assume correct).  The ideal Riffle shuffle is one of those tricks that card masters can use to shuffle cards but end up with the same order (I call that statistically suspect).

 

I also vote 'Shuffle', and NI should make note of the algorithm they use (give credit as well).  (I can say that about a lot of the math/analysis VIs).

 

Like the proverbial tree falling in the forest, changing this name probably won't affect too many Quick Droppers, maybe one.