11-20-2014 12:38 PM
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.
Solved! Go to Solution.
11-20-2014 12:40 PM
Investigate use of Shift Registers.
11-20-2014 12:45 PM
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
11-20-2014 12:48 PM
@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
It would help in the comparing the 3 successive values.
11-20-2014 12:56 PM
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.
11-20-2014 01:17 PM - edited 11-20-2014 01:19 PM
Here's an easy way to get your array and to randomize it (that the Riffle.vi):
Once you have that, just examine the array for groups of odd/even numbers and stop if you exceed three.
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.
11-20-2014 02:05 PM
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.
11-21-2014 06:59 AM
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.
11-21-2014 12:54 PM - edited 11-21-2014 01:14 PM
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.
11-21-2014 01:18 PM
@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.