Equal randomisation of different conditions

Hey, I am quite new to Psychopy and want to create an experiment for my thesis. I have some issues coding because I have dependencies and need overall an equal distribution. I am really grateful if someone could help me here. So, in my experiment, first I have two priming conditions (A and B), which need to be randomly but equally distributed. Then participants will answer questions of random order from either Block 1, Block 2 or Block3 individually. Afterwards, participants will be allocated to one of four collaboration groups (Group 1, Group 2, Group 3 or Group 4). Within this collaboration phase, participants will retrieve one of the three Blocks which was not retrieved before. So for example, if in the individual retrieval the participant received Block 1, the collaboration phase will consist of i.e. Block 2 . Then in the last retrieval phase, participants should retrieve the Block they have not assigned to before. So, referring to the example, now the participant would retrieve Block 3.

How do I make sure that in the end the conditions are equally distributed if I do not know yet how many participants I will have? Also, is it better to code everything/customize it or make more use of the loops? Lastly, for the three blocks, should I have three separate routines in one loop or code all the blocks in one routine?

Thanks so much in advance!

The two ways of balancing independent groups is via the shelf or my VESPR Study Portal (which I still recommend because it has a mechanism for dealing with non-finishers).