Why will this loop iterate just one time not two times? - labview

The Random node will output the random value that is between 0 to 1. The initial i value should be zero. So for the first iteration, the comparator should output a signal TRUE to let the loop iterate again. When i is 2, the loop will stop, because the random variable is smaller than 2.
So as my thinking, the loop should iterate only two times and then stop, but the answer shown to says me the loop should iterate just 1 time. Is the answer wrong or is my thinking wrong?

I ran the same code and got 2 executions of the loop, as you'd expect.
Furthermore, since the random number generator cannot return exactly 1, the code is guaranteed to take exactly 2 loop cycles.

Related

Trouble understanding value vs Series in PineScript

greenCandleCount=0
if close > open
greenCandleCount := greenCandleCount+1
plot(greenCandleCount)
In the above example, greenCandleCount is a value in the first line. But, close and open are both Series of floats. So in line 3, greenCandleCount seems to get converted into series. Because the line 2 comparison operation seems to produce a Series of booleans. Somehow a single variable lying inside an if block where the expression is a Series, also gets expanded into a Series. So why does this happen?
greenCandleCount doesn't get converted to series. The way you've declared it, it was always a series variable. At the start of the next bar, greenCandleCount would be reset to 0
With the code you've provided, greenCandleCount would always be either 1 or 0 and you'd be able to reference the previous bar's greenCandleCount state the same as any other series variable ie greenCandleCount[1]
If you wish to have a variable that is only initialized once and maintains it's value across bars it must be declared with the var keyword ie
var greenCandleCount = 0
Then your code will count the number of green candles on the entire chart.
https://www.tradingview.com/pine-script-reference/v4/#op_var

The operation of a For Loop iterating over an empty array in LabVIEW

I want to ask that why is the Value Out 5 not 4?
In my thinking, "i" value will be 0 at first, then plus 1 and be subtracted by 5, it should become (5-1), that is 4. But the answer is 5. Why?
You wired an empty array to your For loop. When you wire an array to an indexing input terminal of a For loop, LabVIEW automatically takes the length of the array as the iteration number for the loop. It means your For loop doesn't execute any iterations (iteration number = 0).
You wired a value of 5 into the left-hand terminal of your shift register. So, when you push the run button, 5 will be written into the shift register.
Finally, your indicator will read the data in the shift register. Since your For loop doesn't do any work, the value 5 will remain in the register and your indicator will display the value 5.

loop param not working as expected

I have a single clip on the stage with an instance name of testShape. In frame 1 I have the following code:
createjs.Tween.get(this.testShape, {loop:true}).to({y:240}, 1000);
When I run this it loops infinitely as expected but what I want is for it to loop three time then stop and fire a complete event.
The docs say that the loop param...
Indicates the number of times to loop. If set to -1, the tween will loop continuously.
Which suggests I should be able to set {loop: 3} to achieve my desired result but any number value other than 0 just causes it to loop endlessly.
Can anyone advise on what I'm doing wrong or how to make a Tween loop n times before completing?
Cheers all
It looks like TweenJS 0.6.2 and earlier used a Boolean value for loops, so while you can set it to true or false, you can not put it as a number of loops. If you set it to a number, it will be converted to true.
createjs.Tween.get(obj, {loop:true}).to(…).to(…);
This behaviour was updated in version 1.0.0 of TweenJS, in September, 2017.
createjs.Tween.get(obj, {loop:3}).to(…).to(…);
I am glad you found a solution:
I got it working in the end by making each tween set up the next as it completes.

How to prompt messagebox once inside a for loop in vb.net?

Is it possible to display a single messagebox inside a for loop codes?
I am trying to but cannot successfully make it.
Please help.
Thanks in advance.
Code inside a loop, by definition, is meant to execute multiple times, for each iteration of it, so if you don't want to have multiple messages, move that call outside of the loop so it executes only once. This would be the main way of doing so, because it makes it clear that there will be ever only one of them.
Otherwise, you can condition its display, by some particular condition that's warranted to happen only once, possibly caching the result from the user. The second snippet from phadaphunk does something like that.
From you question, I get that you don't understand how it works below.
The loop is a set of instructions that must be executed "x" number of time. So everything that is in the body of that loop will be executed that number of time. Which means that if there are 5 iterations to the loop, you will get 5 messagebox.
This is how you do it if you want a different box for each iterations :
For Each x As Integer In someList
MessageBox.Show(x.ToString());
Otherwise you can't do it unless there is a certain condition that explains to the compiler at which iteration you want the message box to appear. Something like
For Each x As Integer In someList
If (x == 5)
MessageBox.Show(x.ToString());

vba more realistic random number generator

I know I can use a quasi-random number generation function/variable called Rnd. However, I've noticed that whenever I used Rnd in my userform, this sequence of numbers always show up:
first iteration: 0.705547511577606
second iteration: 0.533424019813538
...
As a result, b/c the sequence of numbers showing up are the same every time when I relaunch the userform, it doesn't feel random. Are there other functions in the VBA function set that would make it feel more random? Thanks in advance.
Try adding a single call to Randomize Timer before you do any calls to Rnd. This will seed the random number generator using the time of day.
I don't know a lot about VB but I think you need to seed your number generator. I think Randomize does it for VB.
You could manually set the seed based on the time of day... insert something like this at the top of your vba code:
For i = 0 To (CInt(Format(Time, "ms"))) 'minutes and seconds turned to integer
test_random = Rnd()
Next