Hi, I have build a lottery system where the winner is given a unique gift card code that they can redeem. The problem is that, when two users do the lottery at the same time and they both win, the same code is given to both users.
Here’s my database:
Probability number (0~100)
Unique codes list
Winner list (connected to winner table)
Here’s my workflow:
Generate a random number → if the random number is lower than the probability number, create winner data and add the first item of the unique codes list to it → remove first item from the unique codes list
I assume that when two users are in this workflow at the same time, they pull the same unique code when they win.
Any idea as to how I can ensure that each code is only given out to a single user? Thanks!
Thanks, but I don’t think this would be a solution.
The codes are generated by Amazon, for example, and I save them in by database. I need to give out these codes to the winners, but with the current set up, the same code is given to the two users if they win at the same time.
Is there any way where you’re storing the codes that have been handed out to someone? If yes, you can search these codes and see if there is any overlap.
Create a data type of unique codes, always have a few codes “unassigned”, and just assign the next code when you need it? That way if two users get issued at code at the same time, they get the next code in the queue.