advertisement

Play this slot machine with play money or real money at Bovada

Gold Rush Gus slot machine

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

  1. Reels.  A reel is the vertical spinning thingie.  Modern video slots have five reels, while older electromechanical slots have three reels.
  2. Symbols.  The pictures on the reels.  The player gets paid if s/he lines up a winning combination of symbols.
  3. 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).
  4. 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.
  5. Paytable.  A list of which symbol combinations pay how much.  e.g., Three apples might pay 50 coins.
  6. 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.
  7. 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.
  8. 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

  1. Find, make, or buy a par sheet.  You have various options for getting a par sheet.
    1. FIND A FREE ONE.  I have several that you can use for free.
    2. MAKE ONE.  For a simple slot machine, it's not hard.  See my article on How Slot Machines Work to understand the simple math.
    3. 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.
  2. 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:
    1. Javascript: Crytpo.getRandomValues
    2. Java: java.security.SecureRandom
    3. Unix:  /dev/random
    4. Perl: Crypt::Random
  3. 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

  1. 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).
  2. Repeat step 1 until you have all five stops (or all three stops, if a three-reel game).
  3. Loop through all the paylines, finding out how much was won on each payline for the symbols on that line.
  4. Do the animation, having it stop on each of the five stops your RNG picked.
  5. Notify the player of how much they won, as per the paytable.

Slot machine code (logic) for server-based games

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. The server verifies that the player ID and the player's token match.
  6. 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.
  7. 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).
  8. Repeat step 1 until you have all five stops (or all three stops, if a three-reel game).
  9. Loop through all the paylines, finding out how much was won on each payline for the symbols on that line.
  10. 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).
  11. 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.
  12. Update the player's account balance in the database according to how much they won.
  13. 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.
  14. Clear the flag that says the player hasn't seen the result yet, since by now they've seen the results.
  15. 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.
  • Slot Machine Myths
  • Slot Machine B.S.  Wrong info that's published elsewhere.
  • Strategies. Tips for increasing your chances of winning, and saving money.
  • Slot Jackpots.  Odds of hitting the jackpot, progressive jackpots, and other jackpot topics.
  • 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 malfunctions. How and why slot machines screw up, causing players to think they've won the jackpot when they really haven't.
  • 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!


Practice gambling with play money

Before you throw down your hard-earned cash in a casino, PRACTICE FIRST!  Learn the games with play money where it doesn't cost you anything if you lose.  Seriously.

You can play Bovada's games (below) right away without registering for an account.  Most every other online casino makes you give up your email address just to play the fake-money games — ugh.  That's the main reason Bovada is the only online casino that gets advertising space on my site.  (When you see the registration box, you can cancel it and proceed to the game without registering.)

Play blackjack

Blackjack

Play roulette

Roulette

Play craps

Craps

Play baccarat

Baccarat

Approved by Casinomeister

Proud not only to be an approved Webmeister by Casinomeister,
but also to be one of the very first approved. (#7, May 2020)

Site Contents ©2001-2024 Michael Bluejay Incorporated.
I believe everything herein to be accurate, but I'm not responsible for errors or omissions.  I'm pretty irresponsible, actually.

Home |  About Us   Contact  |  Updates  |  Privacy          Gambling Problem? Call the 800-522-4700 hotline, see horror stories, and know that aripiprazole and Parkinson's drugs encourage gambling.

Play:  Slots  •  Blackjack  •  Craps  •  Roulette