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)

VI High 6: CLAD Exam Preparation on Shift Registers, Zero-Indexing, and the Off By One Error

In this episode, we start a series of Certified LabVIEW Associate Developer (CLAD) Exam Preparation videos. We explore For Loops, shift registers, indexing, waveform graphs, and strategies for taking the CLAD exam.

For more information in preparing specifically for the CLAD exam, visit http://decibel.ni.com/content/docs/DOC-14014
and stay tuned to this series.
For more on learning LabVIEW and being prepared for the CLAD exam, check out the LabVIEW Fundamentals Course offered by Sixclear. To find out more visit sixclear.com.

Experience Level: Basic

(start transcription)

“Passing the CLAD exam is a major life milestone. Understandably, many test takers experience undue trepidation, cold feet, and at times, complete failure of the actual exam. Like a lighthouse on the shore we would like to set your mind at ease by preparing you for the CLAD exam. Let’s begin.
This block diagram shows a common type of CLAD exam question: a screenshot of some code will be shown and the exam question will ask what the final value is of an indicator. For instance, at the end of run time, what will the Waveform Graph on the front panel look like? And you will be given four possibilities, like these.
Remember, when you take the CLAD exam, you won’t have LabVIEW available to run, so you need to let your mind BE LabVIEW. Do you feel it?
Let’s examine this block diagram. First, we see that there is code unrelated to the indicator in question. Look, these indicators off the stacked shift register would not change the value of the waveform graph. The same goes for this comparison and Boolean indicator. Likewise, the indexing of the array happens after the wire branching, so it won’t change the value of this wire, therefore this section is also superfluous. Since we don’t need to worry about what these three sections of code do, let’s remove them for clarity. That’s better. Secondly, we see this auto-indexed tunnel, meaning that we will output an array of values instead of a single scalar. So let’s calculate that array.This For Loop is running 5 times and there is an initial value of 6 entering into the left shift register. We are using the iteration terminal so it’s essential to understand that it’s zero indexed: the first, or really, 0th time the loop runs it will output a zero, incrementing a value each iteration until it finally reaches a value of 4. So when I say the first iteration of the loop, I mean the iteration corresponding to the 0th index coming from the iteration terminal. This confusion over 0 and 1 as the starting point is very common and occurs so often in software programming that it is called the “off by one error”, or O-B-O-E, or OBOE. Your understanding of this concept will definitely be tested on the CLAD exam.
The first loop iteration will add a 0 to the 6 coming from the left shift register and so a 6 is placed in the right shift register and is also the 0 index element of our output array. For simplicity, I will note the array we’re producing as a free label here on the block diagram. I’ll double click to start the label and then hold down Ctrl plus the + sign on my keyboard a few times to increase the font so we can all enjoy viewing the expanding array. While taking the exam, do this with a pencil on the test sheet.
The 2nd iteration, the 6 we placed in the right shift register at the end of the last iteration now starts here in the left shift register, the iteration terminal will be 1, and 6+1 will be seven which is the next element in the array. The 3rd iteration, we will be adding 2 from the iteration terminal to 7 coming from the left shift register to give us a value of 9. The fourth iteration will make it 9+3, or 12. By logical extension, the fifth and last iteration will give us 12+4 or 16, which is the last value in our array.
When runtime is finished, we have an array of {6,7,9,12,16}. How will this look when graphed? On a waveform graph, we’re always plotting a given 1D array on the y-axis against its index number on the x-axis. We see that the first three are very similar, with the correct number of elements, five, starting at 0. Since we’ve calculated the values in our array, we see that A is the correct choice, but you notice that the other 2 options play off the “off by one error” since they are both off by one from the correct answer. One less, and one more. The last answer plays off the “off by one error” in another way, anticipating that you will make an error in the amount of times the loop will run, this shows 6 iterations, 0-5, instead of the five correct iterations, 0-4.
You can see that the CLAD exam will anticipate common programming errors and present you attractive choices corresponding to those. Don’t be fooled, practice and brush up on your LabVIEW Fundamentals and you’ll do well. I know it.”

(end transcription)