Create random list array with specific conditions

tamadon

Active Member
Licensed User
Longtime User
I wonder if anyone can help me with this.

Let's say I have 55 cards and each with 8 images on it. There are 58 total assorted images to draw on all those 55 cards.

The condition is that , on any two cards there must be one and only one image that matches. I know this can be done but not sure how to.

Thanks everyone!
 
Last edited:

ukimiku

Active Member
Licensed User
Longtime User
Please clarify. You have 55 cards, alright. There are 8 pictures on every card, so you have a total of 440 (small) pictures, right? So why do you say that there are "58" total assorted pictures? Sorted by what criterion?

Regards,
 
Upvote 0

tamadon

Active Member
Licensed User
Longtime User
Sorry. There are "58" pictures but only 8 pictures from that 58 can fit on each card.

For example, if the pictures are represented by numbers

Card 1: 1, 3, 5, 7, 58, 14, 33, 40

Card 2: 9, 39, 8, 22, 7, 23, 36, 37

Card 3: 3, 25, 29, 44, 2, 9, 21, 57

Card 4: ...

Card 5: ...
-----------

Matches between cards

Card 1 & Card 2: 7

Card 2 & Card 3: 9

Card 3 & Card 1: 3
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
If I understood correctly, it could be something like this:
B4X:
Dim cardResult(9, 8)
For l = 1 To 8
cardResult(1, l) = l
Next
num = 8
For card = 2 To 8
    For l = 1 To card - 1
        cardResult(card, l) = cardResult(card - l, 9 - l)
    Next
    For l = card To 8
        num = num + 1
        cardResult(card, l) = num
    Next
Next

card = 9
For l = 1 To card - 1
        cardResult(card, l) = cardResult(card - l, 9 - l)
Next
    

Dim rList, rList2 As List
rList.Initialize
rList2.Initialize

For k = 0 To 57
    rList.Add (k + 1)
Next

For k = 0 To 57
    a = Rnd(0, 58 - k)
    rList2.Add (rList.get(a))
    rList.removeAt (a)
Next

For card = 1 To 9
    For l = 1 To 8
        cardResult(card, l) = rList2.get(cardResult(card, l) - 1)
    Next
Next

Haven't test it, so careful...
 
Upvote 0

tamadon

Active Member
Licensed User
Longtime User
Thank you mc73.

However the images should be placed randomly on each card too, not sure if your code do that?

So for each new card created, its image collection has to be compared with all those in the existing cards to make sure only one matching image exist

Also why do you declare Dim cardResult(9, 8) in the beginning instead of Dim cardResult(55, 8)?
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
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.
 
Upvote 0

tamadon

Active Member
Licensed User
Longtime User
Thanks for the explanation, it's becoming clearer now. Now I understand the process of creating card 1 to 9.

But based on the condition, we can no longer create cards 10 to 55 right? So how does the algorithm address that part?

Sorry a bit rusty here...
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
As I've explained, we cannot proceed with card above number 9. So, it's not a matter of the algorithm, but of what exactly we want to do. If for example, you say that you no longer care for further cards to have one common image with the previous, then you can surely 'reset' the algorithm to run from card 9 to say number 17. And so on.
 
Upvote 0
Top