How to generate always different random numbers?

0 favourites
  • 10 posts
From the Asset Store
This Girls Game allows young fashionistas to develop their taste and skills as a fashion designer.
  • I have a 6 random number generator. But no number can repeat itself.

    I use font sprite object for each of them, and each one has an instance variable, starting with 1 to 6. I also have a global score variable that goes up and so each number ends before the next one, until the 6 numbers finish randomize. It doesn't generate all 6 at the same time, first it generates the first, then the second, up to the sixth number. How do I make sure that no numbers are the same? I'll send the capx here. This is a simplified version of a slightly larger project that ran into this problem. I'll post the link here of the larger project on the itch.io page, which I also intend to post here. The objective was to generate random numbers to bet on the mega-sena, but could not repeat the numbers. A user reminded me well, that the numbers are repeating themselves, and that cannot happen. How to solve? Maybe I would have to store each number in a variable, but I couldn't do that either.

    Below the link of the capx project

    https://drive.google.com/file/d/1fxF-XBWxeRM2KtuSDWdWRjNQnkX3JXja/view?usp=sharing

    And below the link of the original project on itch.io.

    https://lincolnsalles.itch.io/random-number-machine

  • I've read advanced random and from what I've seen, I have to use the permutation table, but I still don't understand how to use that in this project. I read but didn't understand :-(

  • Use the create permutation table action with a length of 6. It will generate 6 random numbers without repeats.

  • Here is an alternate way. It checks the value with all the other values and if any match it randomizes the number again and re-does the check.

    dropbox.com/s/z0an316nusgc6nu/unique_random_list.capx

    If you were only concerned with the values 1 through 6 you could do something simpler like this too.

    dropbox.com/s/mb9he4ke03d7r23/unique_random_list2.capx

  • Using the permutation table is quite easy. You initialize it with a length and a starting value, and it'll automatically randomize itself.

    Problem is, you need to create a visual pseudo-shuffling animation. To that end you can still use a normal random call, but when it's time to pick a final number, you do so using the permutation table at the correct index.

    Here's your modified example implementing what I explained :

    drive.google.com/file/d/1HkqVTyFrRjg0AvcsHcTZXT25oTs_D_89/view

  • Here is another example of using permutation tables I made for some other post:

    dropbox.com/s/qschy8ji3bfrvxx/PermutationRandomImages.c3p

  • Using the permutation table is quite easy. You initialize it with a length and a starting value, and it'll automatically randomize itself.

    Problem is, you need to create a visual pseudo-shuffling animation. To that end you can still use a normal random call, but when it's time to pick a final number, you do so using the permutation table at the correct index.

    Here's your modified example implementing what I explained :

    https://drive.google.com/file/d/1HkqVTyFrRjg0AvcsHcTZXT25oTs_D_89/view?usp=sharing

    Dude was really worth it. You showed me that I was making a mistake I was never going to guess, that index 1 of the permutation table is actually 0. How was I supposed to guess? So I was calling the index 1 from 1, when it's actually 0. And apart from that, I just changed your example by putting the table to generate not at the beginning of the layout, otherwise it would always be the same when clicking on the button, but I put it to generate when press the button and the variable is not starting.

    Thanks for all the replies. Many told me that I would have to use matrix for this. Glad you won't need it. This forum is really good.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Here is an alternate way. It checks the value with all the other values and if any match it randomizes the number again and re-does the check.

    https://www.dropbox.com/s/z0an316nusgc6nu/unique_random_list.capx?dl=1

    If you were only concerned with the values 1 through 6 you could do something simpler like this too.

    https://www.dropbox.com/s/mb9he4ke03d7r23/unique_random_list2.capx?dl=1

    R0J0hound

    Thank you very much, I confess that your example was too advanced for me lol. I still don't deal very well with loops, but I'll save it to study this more calmly later on. Thank you for that.

  • Thanks for all the answers, they helped me a lot.

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