Avoid repetion in random combinations

1 favourites
From the Asset Store
Over 2700 files with all the colour combinations from a 15 colours palette!
  • I intend to create some random combinations of symbols distributed in 4 frames of two sprites for the user to identify which are the same and I wanted to know how I can do to avoid that the combinations are repeated in the next plays. Any simple suggestions for this?

    Thanks

  • Not quite following what you are trying to accomplish. But whenever I do non repeating randoms I use an array.

    Store all the combinations - and then remove each combination as it gets chosen from the array, so it is no longer an option.

  • But in this case I need to register all the possibilities in the array, right? I want to create these combinations on fly (randomly)

  • You have 16 possible combinations, right?

    Frame 1 - Frame 1,2,3,4

    Frame 2 - Frame 1,2,3,4

    Frame 3 - Frame 1,2,3,4

    Frame 4 - Frame 1,2,3,4

    Think of these as 4 sets of 4, where the first sprite's frame determines the set from 1-4, and the second sprite's frame determines the number within that set from 1-4

    Since you have 16 possible combinations, what you can do is use Advanced Random to pick a random number from 0-15 (16 digits, starting at 0) without repeating. You can then translate this number into a two different digits. One is a set number from 0-3 (4 digits, starting at 0) and the other is a number within that set from 0-3

    For example, let's say you ask Advanced Random for a random number between 0 and 15 and it gives you 5. We can translate this into two numbers.

    First, let's get the set number. To do this we divide the random number by 4 and round down. This will give us a number that is either 0, 1, 2, or 3. This will tell you what frame the first sprite should be.

    floor(5 / 4) == 1

    So we are in set 1. The first sprite's frame is 1.

    Then, let's find out which frame within that set we are. To do this you can use modulo to find the remainder after dividing by 4. This will also give a number from 0-3.

    5 % 4 == 1

    So this is the frame for the second sprite. Frame 1.

    So 5 translates into Frame 1, Frame 1

    0 would translate into Frame 0, Frame 0

    floor(0 / 4) == 0

    0 % 4 == 0

    1 would translate into Frame 0, Frame 1

    floor(1 / 4) == 0

    1 % 4 == 1

    4 would translate into Frame 1, Frame 0

    floor(4 / 4) == 1

    4 % 4 == 0

    11 would translate into Frame 2, Frame 3

    floor(11 / 4) == 2

    11 % 4 == 3

    15 would translate into Frame 3, Frame 3

    floor(15 / 4) == 3

    15 % 4 == 3

    Each number from 0-15 will give a unique combination of numbers for Sprite 1 and Sprite 2

    Let me know if you have any questions!

  • I've heard advanced random can generate a list of non repeating combinations but I haven't used it.

    Here's one way to generate an array of all the combinations. One idea more in addition to the other answers is you can store random(1) in the first column and then using sort(x) will effectively shuffle the array.

    dropbox.com/s/o2xmtjo8yqkid56/combination_shuffle.capx

    That just loops over that list of pairs which may be fine.

    An alternate idea is to just generate a random pair every time and check it against a history of the pairs of numbers. It will keep generating a random pair till it's unique. For example this is guaranteed to a not duplicate a pair of values for at least 5 turns.

    dropbox.com/s/xptgru4msx1rkz5/combination_history.capx

  • Thank you so much and R0J0hound for the suggestions and examples.

    I will test both solutions and return later if I have any questions or give feedback.

  • I thought it was confusing to apply what was shown and I believe I didn't express myself correctly when I talked about my question. So I decided to talk a little about this stage of the game I'm creating.

    It's a simple password game where there will be symbols (here represented by letters) and answers in numbers that determine the correct position of the letters. The player will have to choose which one shows the correct order among the options.

    Illustrating the information:

    - ABCD (it is the order in which the password appears: A=1/B=2/C=3/D=4)

    - DCBA (is the correct password order)

    Answer options:

    1234

    3214

    4321 (correct answer)

    Both symbols and numerical orders will have to be generated automatically at the same time.

    I'm going to use images, both for the letters and also for the numbers. Maybe I should use a sprite with 2 animations (one with letters and one with numbers) and with 4 frames each, which will be drawn. The issue is that I will have to use instances of the same sprite for all the game items (password in the original order/password reorder numeric sequence/password in the correct order/2 wrong answers and 1 right answer) and thus identify each one of them. Would this be the best solution?

  • Ah, that's a bit different.

    Here's one way.

    dropbox.com/s/k4pdvewd4qbp4y8/shuffle_combinations.capx

    And modified if you want a specific combination included in the mix.

    dropbox.com/s/fv39mxvfsx41vsn/shuffle_combinations_with_a_predefined.capx

  • Ah, that's a bit different.

    Here's one way.

    https://www.dropbox.com/s/k4pdvewd4qbp4y8/shuffle_combinations.capx?dl=1

    And modified if you want a specific combination included in the mix.

    https://www.dropbox.com/s/fv39mxvfsx41vsn/shuffle_combinations_with_a_predefined.capx?dl=1

    Thank you R0J0hound

    I just saw your examples. In this case, the 3 answer options are being drawn, right? Is it possible to include in this same process the original password (randomly drawn letters that will dictate the original sequence (scrambled password)) and the correct password (letters that represent the password in the right order)?

    Just to be clear, the user will see the scrambled password and the correct password and will have to find among the answer options the sequence of numbers that determines the order of the correct password based on the order of the scrambled password.

    For example:

    Shuffled order: CBAD (Order: C=1/B=2/A=3/D=4)

    Correct order: ACDB

    - As "A" appears in the third place of the shuffled order, it will represent the "A" of the correct answer in the first place.(3XXX)

    - The "C" is in the 1st place of the shuffled order, the 1 will take the place of the "C" in the second place of the correct order (31XX)

    - The "D" appears in the fourth place of the shuffled order and in the third position of the correct order. (314X)

    - And finally, the "B", which appears in the second place of the shuffled order and in the fourth place of the correct order (3142)

    Then the system would need to generate one correct answer (3142) and two wrong answers.

    EDITED: I tested CAPX with the predefined combination, and several times the combination I defined did not appear among the 3 answer options. Could you say why this happens?

  • The second example does that. You specify the sequence in the global variable and it’s placed in one of the three groups of three.

  • The second example does that. You specify the sequence in the global variable and it’s placed in one of the three groups of three.

    But would it be possible for me to determine the shuffled order and the correct order through the predefined numerical sequence, remembering that they are related to each other and not only to the numerical sequence? If so, could you explain to me how to do this?

    What about the problem of sometimes not showing the sequence of numbers (frames) that I defined in the variable "answer"/command of the variable "asindex"? Did you get this same issue?

  • I fixed that issue so redownload. It should always show the answer sequence now.

    I don’t understand the first part of your question though.

    Best I understand you have want three four digit answers. One you can specify and the other two are shuffled versions of that.

    I had the digits start at 0 instead of 1, but all the numbers will be shuffled versions of 0123.

  • I fixed that issue so redownload. It should always show the answer sequence now.

    Thanks for fixing the issue

    I don’t understand the first part of your question though.

    Best I understand you have want three four digit answers. One you can specify and the other two are shuffled versions of that.

    I had the digits start at 0 instead of 1, but all the numbers will be shuffled versions of 0123.

    I need to have 5 information on screen that I have to generate:

    - Original order (letters instead of numbers)

    - Correct order (letters too)

    - 3 answers (1 correct + 2 incorrect)

    When I generate the 3 answer options I need to have generated before the original order and the shuffled order (correct order) before.

    If we think only in frames, I need to shuffle the 4 frames of the original order (sprite or animation) and I need to shuffle the 4 frames in the correct order. So I need to create 3 answer options: 1 with the correct order that is the same of the correct order according with the each element of the original order position, and 2 more option with wrong answers (anything but the sequence of the right answer)

    Example

    I shuffle the 4 frames of the original order = ACDB

    I shuffle the 4 frames of the correct order = BDAC

    Correct answer based on the position of each element of the original order: 4312

    In this case, I need to know in which position each frames of the scrambled order sequence is, according to the positioning of these same frames in the original order.

    I hope this is not confusing to understand. Sorry if it is.

  • I really don't understand. It seems to be the same as what the last capx does. I used the digits 0123 instead of ABCD mainly as a simplification. You can set the frame images to whatever.

    Anyways I had another idea on how to do the shuffling. Might me useful.

    Basically a1, a2 and a3 are the three answers. a1 is the correct one. ansIndex is a number 0-2 that offsets the order of the answers. The final step is to display the three answers by converting ABCD to 0123 to set the frames.

    Events 1-7 is enough to generate the answers. You can use any other solution to display them after that.

    dropbox.com/s/fusumitrtn0q3ya/shuffle_combinations2.capx

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I really don't understand. It seems to be the same as what the last capx does. I used the digits 0123 instead of ABCD mainly as a simplification. You can set the frame images to whatever.

    Anyways I had another idea on how to do the shuffling. Might me useful.

    Basically a1, a2 and a3 are the three answers. a1 is the correct one. ansIndex is a number 0-2 that offsets the order of the answers. The final step is to display the three answers by converting ABCD to 0123 to set the frames.

    Events 1-7 is enough to generate the answers. You can use any other solution to display them after that.

    https://www.dropbox.com/s/fusumitrtn0q3ya/shuffle_combinations2.capx?dl=1

    Thank you so much for the effort to help me. I'll make sure to buy you several coffees :)

    Could I continue to use the previous way to shuffle? I wouldn't want to change that now. I'm using your first example as a base. I know this game is a bit confusing and I'm not concerned with its mechanics anymore but with some features of C3 that I can use to improve it.

    A question: I am using use 1 sprite with 4 frames with letters and it will be automatically shuffled. I created another sprite with 4 frames with numbers, also shuffled, and the function of the numbers is to serve as a sequence that I have to order the letter frames.

    For example:

    The first sprite (letters) with shuffled frames resulted in DCAB

    The second sprite (numbers) with the shuffled frames resulted in 3142

    I would have to reorder the instances of the first sprite so that it positions the frames according to the numbers of the second sprite. Would be like this:

    D (3) = would have to go to the third position

    C (1) = would have to go to the first position

    A (4) = would have to go to the fourth position

    B (2) = would have to go to the second position

    How do I distinguish each of the first sprite's instances to change the frames according to the new order?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)