“Winner Winner Chicken Dinner”, and “Vegas, Vegas, Vegas” are quotes from two of my favorite Las Vegas movies. It’s no secret that my wife Kristin and I love Las Vegas, it’s also no secret that I love coming up with ingenious database solutions to random problems. So I was thinking, what it would take to create a simple 3 wheel slot machine in SQL Server. Is SQL Server the right place to put slot machine business logic? Ummmm, probably not, but that has never stopped me before. In this blog post I am going to build a 3 wheel slot simulation using small data set that represents the possible combinations that could appear and a random selection process to determine a pull outcome. I will also add a small table to track the results of each pull including a running balance in a players table.

Slot machine details

A standard 3 wheel slot with 20 stopping position yields 8000 possible result combinations (20^{3} = 8000). If there is only one jackpot symbol on each of the wheels, this means there is a 1 in 8000 chance of hitting the jackpot. If there are 5 empty spaces per wheel, there are 125 possibilities to hit all empty spaces. The simple math is multiply the count of a specific symbol on each wheel. Example, if there are five single bars on wheel number one, two 7’s on wheel number two, and one Wild on wheel three, thus 5 X 2 X 1 hence there are 10 opportunities for this (B-7-W) combination to appear. The total number of distinct combinations can be found by multiplying the count of different possible symbols per wheel. We have 6 different symbols on each wheel, thus 6 X 6 X 6 = 216 possibilities.

Slot machine data set

Armed with the combination logic we can build out a dataset for all 8000 possible combinations. Using an opportunity field, we can store the number of opportunities that each specific combination can appear. See the combinations table for full data set and opportunity count. Combining the opportunity field with a numbers (virtual-auxiliary-table-numbers) table, thanks @ItzikBenGan, we can build out all 8000 combinations. Once we have all 8000 possible combinations we can randomly select a single combination.

Slot machine SQL logic

For this simulation we will need a few tables to store the combinations, the results log for our players, and a table for our players. The combinations table will store symbol for each column, a winning amount for the combination, and the number of opportunities the combination will appear. The results log will store the resulting combination, the winning amount, the player, and a timestamp the slot machine was played. The players table will store the players name and the available funds for playing the slot machine.

Table definitions

**Combinations Table**

CREATE TABLE Combinations ( CombinationID INT PRIMARY KEY IDENTITY(1,1) NOT NULL , Reel1 VARCHAR(5) NOT NULL , Reel2 VARCHAR(5) NOT NULL , Reel3 VARCHAR(5) NOT NULL , WinningAmount INT NOT NULL , Opportunities INT NOT NULL ) ON [PRIMARY]; |

**ResultsLog Table**

CREATE TABLE ResultsLog ( ResultsLogID INT PRIMARY KEY IDENTITY(1,1) NOT NULL , PlayerID INT NOT NULL , Reel1 VARCHAR(5) NOT NULL , Reel2 VARCHAR(5) NOT NULL , Reel3 VARCHAR(5) NOT NULL , WinningAmount INT NOT NULL , ResultDate DATETIME NOT NULL ); |

**Player Table**

CREATE TABLE Player ( PlayerID INT PRIMARY KEY IDENTITY(1,1) NOT NULL, , Name VARCHAR(100) NOT NULL , AvailableFunds INT NOT NULL ); |

Building the Combinations Dataset

Using a CROSS APPLY between the combinations table data and Itzik Ben-Gan’s numbers common table expression will yield 8000 total possible combinations.

WITH L0 AS (SELECT 1 AS C UNION ALL SELECT 1 AS O) , -- 2 rows L1 AS (SELECT 1 AS C FROM L0 AS A CROSS JOIN L0 AS B), --4 rows L2 AS (SELECT 1 AS C FROM L1 AS A CROSS JOIN L1 AS B),-- 16 rows L3 AS (SELECT 1 AS C FROM L2 AS A CROSS JOIN L2 AS B), -- 256 rows Nums AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS Number FROM L3) SELECT Reel1 , Reel2 , Reel3 , WinningAmount FROM Combinations cbn CROSS APPLY Nums nms WHERE nms.Number <= cbn.Opportunities; |

Selecting a winning combination

Using TSQL’s random function we can select a random winning combination. The RAND function takes a seed value to start the randomization process, if the same seed is provided, the same random value will be returned. So we will use the NEWID and CHECKSUM functions to gather a random seed value. To get a value between 0 – 7999, we can multiply the RAND function result with 8000 and cast as an integer. Adding one gives us a random value between 1 and 8000.

SELECT CAST(RAND(CHECKSUM(NEWID())) * 8000 AS INT) + 1 |

Adding a bit of SQL ingenuity we can order the combinations dataset by the random value function. This will completely randomize the combinations dataset. All that’s left to do is select a single record, say the first record.

WITH L0 AS (SELECT 1 AS C UNION ALL SELECT 1 AS O) , -- 2 rows L1 AS (SELECT 1 AS C FROM L0 AS A CROSS JOIN L0 AS B), --4 rows L2 AS (SELECT 1 AS C FROM L1 AS A CROSS JOIN L1 AS B),-- 16 rows L3 AS (SELECT 1 AS C FROM L2 AS A CROSS JOIN L2 AS B), -- 256 rows Nums AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS Number FROM L3) SELECT TOP 1 Reel1 , Reel2 , Reel3 , WinningAmount FROM Combinations cbn CROSS APPLY Nums nms WHERE nms.Number <= cbn.Opportunities ORDER BY CAST(RAND(CHECKSUM(NEWID())) * 8000 AS INT) + 1; |

Time to play the slot

Running the query 5 times yielded the following results.

Reel1 | Reel2 | Reel3 | Winning Amount |
---|---|---|---|

_ | BB | B | 0 |

BBB | 7 | BB | 0 |

B | B | _ | 0 |

B | W | _ | 0 |

7 | BBB | _ | 0 |

Hmmmm… I didn’t win a single time… I think I successfully created a slot machine.

Log the results

All that’s left is to save the results of each spin and update the players available funds. A simple insert into the Results log, including the player, reels combination, winning amount and time-stamp will handle the logging side.

DECLARE @Reel1 NVARCHAR(5); DECLARE @Reel2 NVARCHAR(5); DECLARE @Reel3 NVARCHAR(5); DECLARE @WinningAmount INT; WITH L0 AS (SELECT 1 AS C UNION ALL SELECT 1 AS O) , -- 2 rows L1 AS (SELECT 1 AS C FROM L0 AS A CROSS JOIN L0 AS B), --4 rows L2 AS (SELECT 1 AS C FROM L1 AS A CROSS JOIN L1 AS B),-- 16 rows L3 AS (SELECT 1 AS C FROM L2 AS A CROSS JOIN L2 AS B), -- 256 rows Nums AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS Number FROM L3) SELECT TOP 1 @Reel1 = Reel1 , @Reel1 = Reel2 , @Reel1 = Reel3 , @WinningAmount = WinningAmount FROM Combinations cbn CROSS APPLY Nums nms WHERE nms.Number <= cbn.Opportunities ORDER BY CAST(RAND(CHECKSUM(NEWID())) * 8000 AS INT) + 1; INSERT INTO dbo.ResultsLog (PlayerID, Reel1, Reel2, Reel3, WinningAmount, ResultDate) VALUES ( 529 ,@Reel1 ,@Reel2 ,@Reel3 ,@WinningAmount ,GETDATE() ); |

A simple update will handle the players available funds.

UPDATE Player SET AvailableFunds = AvailableFunds + @WinningAmount - 1 WHERE PlayerID = 529; |

Summary

Obviously this is a simplified implementation of a 3 reel slot machine but it meets the statistical opportunities for all combinations based on the number of symbols per reel. So using some ingenious ideas, a numbers table from Itzik Ben-Gan, and standard T-SQL functionality, we have a functional SQL slot machine that logs its results and keeps players hard earned virtual money. All that’s left to do is book a trip to Las Vegas…

DONE!