# LabVIEW

cancel
Showing results for
Did you mean:

## Random numbers sum

I am doing this for quite some time and I am not able to come up with the solution. Why does not it show the sum of random numbers ? It always show zero. I have seen one similar question on the NI community.

Message 1 of 4
(996 Views)

## Re: Random numbers sum

The Sum of Random Numbers is placed outside of the loop. The value will only be updated once you press the Stop button and exit the loop. To update the value in real-time, place the summation and indicator inside the loop.

-------------------------------------------------------
Message 2 of 4
(966 Views)

## Re: Random numbers sum

As has been said, you need to understand the basics of dataflow. Data is only available once the previous structure has completed, i.e. the while loop in this case. Have you looked at some of the tutorials?

I would also stay far away from resized shift registers, they make the code clumsy and not scalable. For example if your teacher wants you to average the last 5000 random elements in tomorrows problem, you would need to start from scratch. Proper scalable code would only need to change one integer.

• Your shift registers are not initialized, so they will contain stale data on the second run.
• It is incorrect to divide by five for the first few iterations where you have less than five random numbers.
• There is a +1 primitive
• Use arrays for the five random numbers!
• Autoindexing on a while loop is dangerous, because if it runs forever, you'll run out of memory.
• A toplevel loop needs a reasonable wait. It is not reasonable to spin it millions of times per second, building a gigantic, ever-growing array in the output tunnel. The iteration terminal is I32 and can only go to 2^31
• If you are only interested in the final sum, all you need is a scalar in a shift register to keep the running sum. Much more efficient.

For some ideas about implementing a running average, have a look at my 2016 NI week presentation (Part II).

Message 3 of 4
(952 Views)

## Re: Random numbers sum

Here is a simple example how you would sum all random numbers using only a few bytes of constant memory. Be careful, [i] will overflow after a while if the loop runs too fast.

Message 4 of 4
(947 Views)