I'm hoping to use a four character join code so people can join a game. I was thinking of making random codes, which would be about 1.4 million different possible codes. I like the idea but I don't want to repeat one by accident.

I'm using Firebase for my database, so I was thinking of just storing all the combinations in a document and grabbing the next in line.

These both sound not-so-great. It seems like there's a better way. Thoughts?


1 Answer 1


Store the active codes in the database. Once they are no longer active, you remove them.

To create a new code, you can generate the code by whatever means you want (I'm going to suggest to use a random so people can't guess what the next code will be). Then you want to add it to your database as long as it was not there already. If it was already there, then generate again.

I'm assuming these codes can be claimed by anybody. I want to encourage to add password support. Because even if people would have a hard time guessing the codes, that only gives you security by obscurity.

  • \$\begingroup\$ I need a bit more clarification. By "active codes" do you mean codes for games that are being played or have already been played, or do you mean all the possible, available codes? \$\endgroup\$
    – tazboy
    Mar 16, 2022 at 15:08
  • 1
    \$\begingroup\$ @tazboy They are the codes you can draw one from and assign to a new game for players to join. \$\endgroup\$
    – liggiorgio
    Mar 16, 2022 at 15:32
  • \$\begingroup\$ @tazboy Available. Not all possible codes. If it were all possible codes, then it would make no sense that I tell you to generare more and them there. I don't know what "being played" means, aren't they to join? - You can store other data with code if it makes sense, such as an user id associated with it (e.g. for an invitation that can only be claimed for one person), or an expiration date, or hashed password. Or, if there codes that can't be used but you can't remove them yet, you could store a "reserved" flag or something. So this approach can be adapted to "being played" if you need. \$\endgroup\$
    – Theraot
    Mar 16, 2022 at 21:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .