advertisement
Play this slot machine with play money or real money at Bovada
No popups, no download, no registration, no B.S., just the game. One click and you're in.
How to Program a Slot Machine
aka Slot Machine coding
Last update: March 7, 2023
This article explains how to code the math part of a slot machine to get the desired payback / RTP. It does not cover anything about the graphics or user interface, you'll have to find some other article to cover those things.
First let's make sure we have all our terms straight.
Terms to understand
- Reels. A reel is the vertical spinning thingie. Modern video slots have five reels, while older electromechanical slots have three reels.
- Symbols. The pictures on the reels. The player gets paid if s/he lines up a winning combination of symbols.
- Stops. The stops are each place on the reel that shows a symbol. Here's a tiny reel with just ten stops: apple, banana, cherry, cherry, banana, apple, apple, cherry cherry, apple. This reel has three symbols (apple, banana, cherry), but ten stops (places that a spin can stop on).
- Paylines. A line that goes from left to right through all the reels, going through one symbol on each reel. This is easier to see that to understand from text.
- Paytable. A list of which symbol combinations pay how much. e.g., Three apples might pay 50 coins.
- Par sheet. The "blueprint" for a slot machine. It's a list of the symbols on each reel, and a paytable. The combination of the reels and the paytable is what determines the RTP.
- RTP / Payback. This is how much the machine pays back over an infinite amount of play. RTP stands for "Return to Player". Most real-money online slots pay back around 96%. Here's my detailed article about RTP.
- RNG. Random number generator. This is an algorithm that picks random numbers. Note that the very nature of using a formula to pick numbers means that the numbers aren't perfectly random, so computer RNG's are actually "pseudo random numbers", so an RNG is often called a PRNG. Even so, good PRNG's pick numbers that are indistinguishable from truly random numbers.
Preparation
- Find, make, or buy a par sheet. You have various options for getting a par sheet.
- FIND A FREE ONE. I have several that you can use for free.
- MAKE ONE. For a simple slot machine, it's not hard. See my article on How Slot Machines Work to understand the simple math.
- BUY ONE. You might need features that you can't find in a free par sheet and that are beyond your ability to design yourself (usually a specific RTP). In that case you can hire a mathematician or slot-math specialist to make one for you. I offer this service if you need it.
- Choose an RNG. If your game isn't played for real money, you can use whatever RNG comes with your programming language; it's good enough. If money is on the line then you need a cryptographically-secure PRNG, called a CSPRNG. Examples:
- Javascript: Crytpo.getRandomValues
- Java: java.security.SecureRandom
- Unix: /dev/random
- Perl: Crypt::Random
- Decide on client-based or server-based. If there's no money on the line, put all of the code on the client (e.g., web browser). That way the game isn't using any of your server resources, and the game will work even if the player's Internet connection drops. If there's money on the line, then everything should be on the server except the GUI. It's a security risk to have the RNG or the math for the player's balance on the client. The server should generate the random result and send that to the client, and the server should also determine how much the player won and add that to her/his account.
Slot machine code (logic) for client-based games
- Get the stop for the first reel. To do that, pick a random number between 1 and the number of stops on the reel (e.g., 128).
- Repeat step 1 until you have all five stops (or all three stops, if a three-reel game).
- Loop through all the paylines, finding out how much was won on each payline for the symbols on that line.
- Do the animation, having it stop on each of the five stops your RNG picked.
- Notify the player of how much they won, as per the paytable.
Slot machine code (logic) for server-based games
- When a player creates an account, create a token and store it in the user's database record, to verify the player's identity when the player's client sends request to the server. This prevents hackers from sending bogus requests by trying all possible player IDs. The token is not the player's personal password.
- Store that token as a cookie in the player's browser or app, so the client can send it to the server along with the player ID.
- When the user loads a game, the client sends an encrypted request to the server, with the player's ID and token. The server fetches the player's $/€ balance and sends that to the client to display in the game.
- When the player clicks the Spin button, send a request to the server, encrypted, with the player ID, the player's token, the game ID (which slot), the amount bet, and which lines were selected.
- The server verifies that the player ID and the player's token match.
- The server verifies that there's enough money in the account to make the bet. There should be, because the client shouldn't allow a bet to be made with insufficient funds, but a hacker could send a request for a bet that exceeds the player balance. That's why we check the balance first.
- Get the stop for the first reel. To do that, pick a random number between 1 and the number of stops on the reel (e.g., 128).
- Repeat step 1 until you have all five stops (or all three stops, if a three-reel game).
- Loop through all the paylines, finding out how much was won on each payline for the symbols on that line.
- Log the result. Logs might be required by regulators, and are certainly a good idea to be able to check players' activity and to make sure the games are working properly (e.g., returning expected RTP).
- When logging the result, set a flag to indicate that the player hasn't yet seen the results. That way we'll be able to show the results if the player gets disconnected before seeing them.
- Update the player's account balance in the database according to how much they won.
- Send instructions to the client to do the animation (having it stop on each of the five stops your server-based RNG picked), show the winning lines, and tell the player how much they won.
- Clear the flag that says the player hasn't seen the result yet, since by now they've seen the results.
- When a player logs in check to see if the "not seen result yet" flag has been set, and if so, then run the animation, notify the user of how much they won, and clear the flag.
Progressive Jackpots
The PAR sheet tells you the progressive contribution. e.g., If it's 2%, then grow the jackpot meter by 2% of each bet placed.
"Must-Hit-By" Progressive Jackpots (MHB)
Set the secret value that the jackpot will be awarded at, by picking any value between the reseed value (jackpot starting value) and the maximum value (the must-hit-by value). The value doesn't have to be chosen uniformly from all values between min and max, it can be biased towards the max value (which is how most slot makers do it).
Complete working example (Javascript)
Here's the complete code for a Javascript slot machine (sans graphics). This is for a 3-reel, electromechanical style. The principle is the same for a five-reel slot. For five reels, just use two extra reels, and put symbols on every stop (instead of a blank every other symbol), as per a par sheet .
Play slots online
I suggest you play something other than slots because slot odds are so bad. You could also play online with fake money, because then it doesn't matter if you lose. A good casino for free-play is Bovada, since it requires no download and no registration. (If you see a registration box, you can close it and continue without registering.) You can play with real money too, though I hope you won't (or at least won't bet more than you can comfortably afford to lose). (advertisement)
All my slot machine articles
- Slot machine basics. How much it costs to play, how much you can win, expected loss, why they're a bad bet, why they're popular, how you can limit your losses, speed of play
- How to play slot machines
- Slot returns. How much they pay back.
- The Randomness Principle. Slots don't continually get looser and tighter as they're played. They don't have to.
- How they work. Explains the randomness principle, and runs through the math to show how a game returns a particular payback percentage. There's a companion page on Par sheets.
- Strategies. Tips for increasing your chances of winning, and saving money.
- Slot Jackpots. Odds of hitting the jackpot, progressive jackpots, and other jackpot topics.
- Bonus Rounds (all about them)
- How to Beat Slot Machines, aka Advantage Play (AP). It's not what you think: it's not easy, and for the most part, not lucrative.
- Slot Machine malfunctions. How and why slot machines screw up, causing players to think they've won the jackpot when they really haven't.
- Slot Machine Myths
- Slot Machine B.S. Wrong info that's published elsewhere.
- Biggest Jackpots. The largest slot and table game jackpot wins in Vegas.
- Skill-Based Slots. The scoop on the new games in which your results aren't entirely determined by chance.
- Slot Machine Simulator. I programmed an exact replica of the Blazing 7s slot (odds-wise). Click it to play thousands of spins in one second and see how you do.
- Slot name Generator. Randomly creates a slot machine name using common slot words. Hilarious!