Suppose for a moment, that we don't care for random numbers, but we simply have a series of 58 pictures, from 1 to 58.
We want to put them in cards, 8 images per card. We also want each card to contain one and only one common image with another card.
Here's a simple way to achieve this.
Card 1:
1,2,3,4,5,6,7,8
In Card 2, we start with image number 8. This way we have one common image with card 1.
Card 2: 8,9,10,11,12,13,14,15
In Card 3, we start with image number 15. This way we have one common image with card 2. BUT, this time, we want also a common image with card 1. We can simply select, the image nr 7 (previous to nr 8).
So,
Card3: 15,7,16,17,18,19,20,21
Continuing with card4, we can begin with the last image of card 3 (21). We also want a common image with card2. We can select card 14. We want an image from card1. We can select 6.
Thus,
card4: 21,14,6,22,23,24,25,26
This way, we proceed, until card nr 9. Think about it, a little. This card, has to have one common image with all the previous cards. Thus, all its 8 components, are chosen from the previous cards. Thus, There is no space left for another card, based on the conditions chosen.
This was the reason I dimmed 9 cards, with 8 elements each.
Anyway, having finished with executing the algorithm, we now have to randomize our numbers. That's easy: take each number and point it to another, a random one. Everytime, we do that, we have to take out of our list, the chosen random number, in order to get a fresh one next time we loop.
Finally, we replace every serialized number with its correspondent random one.
This was the process followed in the code I posted.