LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Array with no more than three even or odd numbers in succession

Solved!
Go to solution

Hi,

I need help with the following vi:

The vi should generate an array that has the numbers 1-20 in a random order with the restriction that there are no more than three even or three odd numbers in succession.

I have no idea how to achieve it. I could generate random numbers in the 1-20 interval, but how do I replace the repeats AND meet the requirement for no more than three odds/evens in a row.

Can somebody provide a sample vi or other advice please.

0 Kudos
Message 1 of 14
(4,566 Views)

Investigate use of Shift Registers.

0 Kudos
Message 2 of 14
(4,560 Views)

For the most part I know how to use shift registers, but I do not see how a shift register would help accomplish this task Smiley Embarassed

0 Kudos
Message 3 of 14
(4,554 Views)

@Broni wrote:

For the most part I know how to use shift registers, but I do not see how a shift register would help accomplish this task Smiley Embarassed


It would help in the comparing the 3 successive values.

0 Kudos
Message 4 of 14
(4,551 Views)

Each time you generate a new number, you could search the array to see if it is already in it. If it is, dont add it and go through the loop again. If you have established that your number isnt already in the array, then you could check the last two numbers to ensure that you wont violate the 3 odds or evens. Use case structures to decide what to do for the cases. Since you will need to go through with way more than 20 iterations to fill up your array, you will need two shift registers, one for the array, and one keeping track of the array index. 

0 Kudos
Message 5 of 14
(4,547 Views)

Here's an easy way to get your array and to randomize it (that the Riffle.vi):

 

Odd%20and%20even%20array[1]_BD.png

 

Once you have that, just examine the array for groups of odd/even numbers and stop if you exceed three.

 

Odd%20and%20even%20array[1]_BD.png

 

If you do exceed the limit, re-riffle the array and try again.  It'll eventually get it right.  I set it to allow only one in a row and it took no more than half a million tries.

 

Odd%20and%20even%20array[1]_FP.png

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 6 of 14
(4,535 Views)

Jim,

I love the solution with the riffle.vi. Thank you.

Still I can not quite figure out the groups of three sorting.

Can you please help me.

Thank you in advance.

0 Kudos
Message 7 of 14
(4,512 Views)

Odd%20and%20even%20array[1]_BD.png

 

Take the riffled array and look at each element (I used an auto-indexing For loop).  If it's odd, add one to the "odd" shift register and put a zero in the "even" register (I let the Case structure's output tunnel insert the default value).  Do the opposite if it's even.  Add the odd and even counts, compare the sum to your limit then stop comparing if it's been met and try again.  If you make it through all 20 without exceeding your limit, the outer loop will exit and you've got your answer.

 

Is this a homework assignment?  It's OK if it is; I'm just curious.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 8 of 14
(4,471 Views)

Here is what I came up with. I think jcarmody's solution is a much cleaner looking VI, but this should work as well, and usually finds a solution in under 100 iterations. 

 

 

Edit: After playing with it a few times, every couple runs, it would do something funky and run continously. Its because if the last two numbers are both even or odd, and the final number to put in is also even or odd, it will never put it in. So, something to check for that and re start would be needed to get this vi to run properly. It only happens every couple times, so if you implemented that you could still get it to complete in probably less than 1000 iterations. 

0 Kudos
Message 9 of 14
(4,448 Views)
Solution
Accepted by topic author Broni

@paulrr wrote:

[...] usually finds a solution in under 100 iterations. 


I just ran mine a million times and the most it took was 31 iterations.  The vast majority were much quicker.

 

Odd%20and%20even%20array[1].png

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 10 of 14
(4,435 Views)