LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to split a array into two random arrays of equal elements

Solved!
Go to solution

Hello everyone!
I'm learning to code with Labview and I can't find a simpler solution to a problem I got, but I'm sure that there must be a simpler one!

 

Basically I got a .txt file with a list of soccer players and their role. I read it and convert it into a 2D array then I have to split the players (the 2D array) into two random teams of equal players (two random arrays of equal elements).

 

For example, in the list, I have 2 goalkeepers, 6 defenders, 8 midfielders and 6 strikers. By the end of the split, I will have to end up with two random teams of 1 goalkeeper, 3 defenders, 4 midfielders and 3 strikers each.

 

I attach the code I came up with, it works but I'm looking for a smarter and simpler way to do it, and the file .txt

 

Thanks for helping!!!! 

 

 

Download All
0 Kudos
Message 1 of 7
(2,732 Views)
Solution
Accepted by topic author UncleSamItaly

Hi Sam,

 

too much duplicated code.

Core component to randomize and split an array of strings:
check.png

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 7
(2,717 Views)

Hi Gerd,

Thank you so much for the quick response!

It does work really well but I do still have a problem.

Basically, if I do not split the array with all the players at the beginning (before using your code to generate two random teams) into 4 different arrays of

- 2 goalkeepers

- 6 defenders

- 8 midfielders

- 6 strikers

I do not end up with two teams with 1 goalkeeper, 3 defenders, 4 midfielders, 3 strikers each.

It is not a big deal since the solution you provided for me is really straight but I do have still to copy and paste the code 4 times (one time for each role) and then merge the arrays together to have the team with the right number of players per role. I attached the example.

 

Also, sorry for this last question, is there a chance I could use a low-level function instead of the Riffle function?
Thank you so much for helping.

 

 

0 Kudos
Message 3 of 7
(2,691 Views)

Don't copy code.  Create subVI's!

 

But you can still use riffle.  Riffle them up.

Go through the list one by one.  Add a player to a team and keep track which position they were by incrementing a counter for each position.  (Or start with how many you're allowed and subtract 1.)

Once you get to a player and you've met the limit for that position, add them to the other team.

Once you've met the limit for all the positions, add all the remaining players to the other team.

 

Message 4 of 7
(2,682 Views)

Hi Raven,

OMG, you're totally right about the subVI's thing!

I don't know why I didn't think about it before since I was already coding this as a subvI. Thank you!

 

Also, is it the counter method that you are referring to similar to the code I made at first? Because yes, it works, but it doesn't look like a simple way to do it. (i will reattach the first code I made).

 

 

Message 5 of 7
(2,679 Views)

Btw,

Thank you so much, everyone!

I think that the easiest way will be to use the code that Gerd proposed, after previously splitting the players in each category, and merging the 4 random arrays together at the end.

 

I think I also found the "homebrew" solution to the Riffle function that I was looking for.

 

p.s. I don't know why but it won't add the "randomize.PNG" to the message so I did attach it. It is the picture of the solution for a "homebrew Riffle".

 
 
 
 
 
0 Kudos
Message 6 of 7
(2,668 Views)

@UncleSamItaly wrote:

 

Also, is it the counter method that you are referring to similar to the code I made at first? Because yes, it works, but it doesn't look like a simple way to do it. (i will reattach the first code I made).

 


I had a hard time figuring out what your original code was doing because it was too busy.  I doubt you did what I was talking about in that code because if you had, you could have done it in a fraction of the code as you attached.

 

It would have been 1 loop and a few shift registers.

0 Kudos
Message 7 of 7
(2,628 Views)