LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Running Average of Random numbers

Solved!
Go to solution

Hi,

I need to design a program that displays a random number between 0 and 1 once every second. Then it will compute and display the average of the last four random numbers generated. Display the average only after four numbers have been generated; otherwise display a 0.  Each time the random number exceeds 0.5, let a LED turn on. 
Please help me. Or a flowchart of things i need to follow would be really appreciated
this is what i did so far and its not right 😞

 

Download All
0 Kudos
Message 1 of 11
(9,170 Views)
Solution
Accepted by topic author Alyssa001

In order to average the last four numbers, you could keep the last four numbers in an array of size 4 in the shift register, always replacing the oldest element and averaging the current elements once at least four elements have been added (ask the iteration terminal ;)). This should get you started. There is also mean-ptbypt, but I think your teacher wants you to code it from scratch. Adding the comparison and LED seems trivial. Don't use a FOR loop. Everything need to happen in a while loop that runs forever or until the program is terminated.

0 Kudos
Message 2 of 11
(9,149 Views)
Solution
Accepted by topic author Alyssa001

rn.png

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 3 of 11
(9,114 Views)
Solution
Accepted by topic author Alyssa001

@paul_cardinale wrote:

rn.png


 

Using an expanded shift register is a horrible kludge, because it is not really scalable and makes you rewrite the code basically from scratch whenever you want to change N. My suggested solution is much more scalable, because changing the averaging length only requires changing a single diagram constant. Of course "mean ptbypt" does the same thing as you can see by the comparison here. So all you would use is the lower part.

 

Currently, it always outputs an average but It would be trivial to modify it so it outputs zero based on [i] and N, try it. Now you don't even need the min&max function. 😄 

 

movingaverage

 

For even fancier methods, e.g. when using very large history averages, you can avoid certain computations to save CPU. For example instead of summing all data with each iteration, it would be sufficient to also keep the sum in a scalar shift register and, with each iteration, subtract the oldest and add the newest value.  Have a look at our 2016 NI week presentation

 

0 Kudos
Message 4 of 11
(9,104 Views)

Thank you Sir. It was extremely helpful 🙂

I figured out how to average zero till the 4th iteration but connecting a comparing tool to the iteration counter.

 

This is my first labview class. and we did not go over array and other tools you have mentioned such as ptbypt (Which i couldn't find by going into the probability palette :D, let me know where it is located if possible :D) 
 

0 Kudos
Message 5 of 11
(9,088 Views)

This is great  🙂
Thank you so much. As it is my first labview class, it is very simple and easy to understand so early in the semester 😄 

Quick question though,

if i understand the function of shift register correctly, it carries the value of the first iteration and gives it to the next 

So if the follow the wire connection, at first random number generator creates a number, it goes into the compound arithmetic function and takes the first of the 4 places, and also goes to the 2nd iteration thru the shift register which is connected to the 2nd place of the compound arithmetic function. So aren't we using the same number twice.

It is probably a very dumb question and i am sorry to bother you experts with such nonsense.
I really apologize for that. I am trying to understand and not just copy it 😄

0 Kudos
Message 6 of 11
(9,082 Views)

Sir,

 

I am sure this will work. But as i am very new to labview, there are few functions  that you have used is something i am not fimiliar with. I am trying to read the book, using help function to get myself familiarized with those. And after that i will try this way as well. Thank you very much 🙂

0 Kudos
Message 7 of 11
(9,076 Views)

@Alyssa001 wrote:

So aren't we using the same number twice.


Each number will be part of four different running averages, so yes, each number is used four times in a row.

0 Kudos
Message 8 of 11
(9,073 Views)

Here's what I would do. It is probably more reasonable to output NaN until four values have been acquired instead of outputting zero. It simplifies the code and once in a gazillion years, zero (withing five significant digits) could be a valid average. 😄

 

4PointAverage.png

 

0 Kudos
Message 9 of 11
(9,070 Views)

After seeing your code, I remembered something I came up with a long time ago,  See attachment.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 10 of 11
(9,028 Views)