How to generate game pins

I am developing a game that requires a game PIN to join, similar to Kahoot in that regard I suppose. E.g. KA1PN2XR. I was thinking of making it 8 characters and alphanumeric, so I'm not worried about running out of combinations as there are 36^8 or about 2.8 trillion (capital letters) of them, and games will be removed once everyone has left.

However, I am wondering what is an efficient way to generate these pins? Should I use a while loop that generates one until it finds one that isn't in the database? Seems inefficient but I can't think of a much better method.

Thanks.

1

3 Answers

You can do this with a simple function like this:

import random w_list = 'abcdefghijklmnopqrstuvwxyz1234567890' #characters you want in string def generate_pin(length=8): tp = random.sample(w_list, length) pin = ''.join(tp) if pin not in prev_list: #list of already generated pins prev_list.appen(pin) return pin else: generate_pin() 
2

Rather than a loop that generates one and checks to see if it's in the database, it would probably be more efficient -- and safer -- to include a unique index in the database and catch the exception in the unlikely event of a collision.

For example:

SQL table:

create table game_pins ( pin varchar(8) primary key, ... ) 

Python:

import random, string for retry in range(10): # 10 Retries pin = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(8)) try: (code to insert in database) return pin except (database exception for duplicate key): pass raise Exception('Too many retries generating PIN') 

The reason this is better than a loop that checks first, then inserts, is that you're relying on the database to keep the PINs unique.

3

if you make a random pin of 8 chars with slicing of uuid like this:

import uuid print str(uuid.uuid4())[:8] 

regarding of deleting pin after finishing game, and assuming you have 1000 users per second and each game lasts 30 minutes, probability of repeating of a pin in a second is approximately zero: (1000*30*60)/(35^8)=7.99333749768639e-07 35 is number of alphanumeric states and 8 is number of slicing chars from uuid. you can increase or decrease number of slicing chars based on estimating of pins generated in a second and number of users and risk of repeating.

in the worst case if you generate a uuid with 32 chars and not deleting them: after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%.

check the uniqueness of these pins is very inefficient solution for problems like these. you are deleting pins after game, and this is the heaven of random key generation algorithmes that their goals are not generating duplicate key in long run generation.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like