VI High 14: How to Select Random Elements from an Array in LabVIEW

In this episode we are preparing to select the winners of our sweepstakes giveaway. What better way to make the random selection than a LabVIEW VI?
None.
Join us as we look at how to randomly select items from an array and how to avoid the pitfalls of this seemingly simple task.

For more on learning LabVIEW, check out the LabVIEW Fundamentals Course offered by Sixclear: sixclear.com.

Experience Level: Basic

(start transcription)

“Avid Sixclear paparazzi are already well aware that entry to our sweepstakes giveaway of six licenses of the Sixclear LabVIEW Fundamentals Online Course is days away from coming to a close. As such, we need a way to randomly select the six lucky winners from the eager entrant pool. What better way than LabVIEW?
Our task is well defined. We will have an array of strings with names of our entrants. From that array, we will randomly select 6 with LabVIEW. There are several ways to implement this, we’ve chosen the most hopelessly interesting.

We will implement a solution in the following manner:

1. Generate 6 random numbers
2. Scale the random numbers by multiplying them by the size of the name array, this will generate an array of indices that can be used to pull out random names from the name array
3. Check for and replace duplicate indices
4. Use the indices to generate an output array of 6 random names

Let’s head to LabVIEW and do it.

I’ve already created a multicultural array of names on the front panel that we’ll use. Generating the 6 random numbers is easy. Just put a Random Number function in a For Loop running 6 times and auto-index the output. That occurs by default. Now to scale those to the size of the name array, we’ll take an Array Size function and use the polymorphic attribute of the Multiply function to multiply the array and the scalar output together. Now these numbers coming out are extremely precise fractional numbers and we just want whole numbers corresponding to indices, so we’ll use the Round Toward -Infinity function. For now, we’ll skip step 3 and just use this array of indices to generate our output names array. We’ll auto-index this array into a For Loop and pass the index to an Index Array function. The array we’re indexing is the full Name Array, and we’ll want to disable indexing on this tunnel coming in because we want that full array. Each iteration of this loop will give us one of the selected names, so we’ll auto-index the output to generate the full list of 6 names.

If I run this a few times we see that this procedure does indeed work except that we sometimes run into duplicate indices as a result of rounding down each element. You can imagine that multiple numbers in this array could be rounded down to the same number and so we have duplicates. Obviously we have a small Name Array and so we’re more likely to get duplicates, but we need a robust way to find and eliminate those duplicates for any size array coming in. Remember, robustness.

And remember that until next time because we are out of it. Uh, that’s time, not robustness, still plenty of that. In the meantime, you can enter the sweepstakes until June 9, 2011, just check out the entry requirements on our news blog: news.sixclear.com.”

(end transcription)

  1. vihigh posted this