Showing results for 
Search instead 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.





0 Kudos
Message 1 of 4

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.

Control Lead | Intelline Inc
0 Kudos
Message 2 of 4

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.


Other problems in your code:

  • 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).




0 Kudos
Message 3 of 4

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.




0 Kudos
Message 4 of 4