LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

VI that calculates sum of random numbers

Hi guys!

I am studying for a midterm and am trying to create a VI that finds the sum and average of 1,000,000 random integers. I want to display the last 5 numbers at least. How would I do that?

I already have the averages figured out, I just need to figure out how to display the last 5 numbers

Thank

0 Kudos
Message 1 of 16
(4,891 Views)

Your VI does not generate random integers, but random floating point values. There should be very little orange (just near the dice and for the average!).

 

You should keep the running sum in a shift register.

 

Use an array of 5 elements in a shift register to track the last five. Use rotate and replace to append new values. Of course your loops spins so fast that there should not be any indicators inside the loop. It is sufficient to display the last five and the average once the loop has completed.

 

0 Kudos
Message 2 of 16
(4,878 Views)

Here's one possibility (note that the last five are not necessarily in the actual order, but that seems irrelevant).

 

Note that if the random number range is significantly larger or the count much higher, you might want to use I64 instead to prevent overflow in the sum.

 

Currently, it generates 0..99, but it would be easy to change to 1..100. Modify as needed.

Make sure you understand the use of Quotient&remainder. We basically replace the oldest value with the newest with every iteration of the loop.

You should also make sure that the control has a typical default value (e.g. 1M) before saving.

 

randoms.png

 

 

 

Message 3 of 16
(4,861 Views)

Even though I never use them (because the don't scale well) an easier solution for the last 5 would be to resize the shift register to show the last 5 elements, then build array and pass out the last value (which would be the last 5)

0 Kudos
Message 4 of 16
(4,818 Views)

@Hooovahh wrote:

... to resize the shift register to show the last 5 elements,...


Yes, not only does this scale poorly (you would need significant code changes to e.g. show the last seven, and it gets unmanageable to show the last 1000, etc.), it is also inefficient, depending how well the compiler can manage inplaceness. My code only needs to touch one history entry per iteration.

 

If the last N entries should be sorted by occurrence, we can do a final rotation based on the last R after the loop has completed.

0 Kudos
Message 5 of 16
(4,811 Views)

I would of used the Array Subset after the for loop to find the last N values.

---------------------------------------------
Certified LabVIEW Developer (CLD)
0 Kudos
Message 6 of 16
(4,798 Views)

@Frozen wrote:

I would of used the Array Subset after the for loop to find the last N values.


That would require first building the entire array containing all random numbers (e.g. via autoindexing), allocating massive amounts of memory just to throw away 99.9996% of it at the end. Seems very wasteful. 😉

0 Kudos
Message 7 of 16
(4,792 Views)


That would require first building the entire array containing all random numbers (e.g. via autoindexing), allocating massive amounts of memory just to throw away 99.9996% of it at the end. Seems very wasteful. 😉


Yep. I knew there was a reason.

What is that saying ... "the fastest way to get the right answer to a question is to post the wrong one" LOL

---------------------------------------------
Certified LabVIEW Developer (CLD)
0 Kudos
Message 8 of 16
(4,784 Views)

Ever thought about using a Finite Queue with Lossy Enqueue?  When you define the Queue, you set the length of the "shift register".  As long as you enqueue more than the length of the Queue, it will hold the "last N" (in order, "youngest" first) and is reasonably fast.  [I've used this as a "poor man's Running Low Pass Filter].

 

Bob Schor

0 Kudos
Message 9 of 16
(4,778 Views)

How about something like this? It only does 100 integers but you can change that easy enough. Sure it's not scalable but it's really a BS application to begin with... And the instructor is probably looking for a BS answer involving shift registers like this.

 

100Capture.PNG

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 10 of 16
(4,753 Views)