2

So, I saw this.

How are actually these random numbers generated, and how are they actually translated further on?

I was planning to make a basic slot machine, but have no idea where to start.

Well, I had an idea to use something like

random_number_1 = rand(1, X);

for each slot, but that doesn't seem very effective. How to solve that?

By the way, here is one image from the link above, that I'm concerned with:

enter image description here

Edit: Would it be the same style functionality with a slot machine with 3x3 instead of 1x3 lines?

ENIAC
  • 813
  • 1
  • 8
  • 19
maria
  • 207
  • 5
  • 22
  • 56
  • see the legendary: [Understanding “randomness”](https://stackoverflow.com/a/3956538/2521214) so you simply use uniform pseudo RNG to construct distribution you want ... there are number of ways to do it ... combining math operations on uniform distribution or [checking against arbitrary distribution curve](https://stackoverflow.com/a/22422035/2521214) ... If you want to create your own RNG see [How to seed to generate random numbers?](https://stackoverflow.com/a/29296619/2521214) – Spektre Jul 14 '19 at 10:08

2 Answers2

3

It depends on how realistic you want the slot machine to be. If you just want it to randomly pay out, then a simple r = rand(1,X) is fine. That's a good way to start because you can then prove that the mechanics of your game work: all the graphics and animations display correctly, the user interface controls work as expected, etc.

But a real slot machine doesn't just randomly display every possible combination. For example, if three sevens pays out more often than three cherries, then the slot machine is designed to show the three sevens much less frequently. After all, the goal of the slot machine is for the house to make money.

The math behind slot machines is quite involved: much too involved for a Stack Overflow answer. How you generate the random numbers, assuming you get a uniform distribution, is not a major concern compared to setting up your wheels to give a consistent payout.

My suggestion is that you use your built-in random number generator until you get your game working. Then, if you want it to work more like a real slot machine, type "slot machine math" into your favorite search engine, and start studying.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • 1
    Your description of slot machine payouts is misleading. State gambling boards heavily regulate the operation of slot machines, and they are required by law to behave randomly, showing symbols according to the frequency of each, which must be made available to the public, and each "wheel" must be independent. So they really do "randomly display every combination". Payouts are then based on the odds of certain combinations appearing, not the other way around. – Lee Daniel Crocker Jul 15 '19 at 21:54
  • @LeeDanielCrocker Thanks for the note. I'm somewhat familiar with how the machines are designed, built, programmed, and regulated. You're correct that I over-simplified things, and that my description could be misinterpreted. The key point you made is that symbols are shown at random according to their frequencies. I was trying to convey that same idea, but failed. – Jim Mischel Jul 16 '19 at 03:03
1

I would use the old tried-and-true Fisher-Yates shuffle algorithm for this. This question has a JavaScript implementation, and it can be used for slots like so:

let slotOptions = ["cherry", "seven", "bell", "bar"];

function play() {
    return [shuffle(slotOptions), shuffle(slotOptions), shuffle(slotOptions)];
}

let [slot1, slot2, slot3] = play();

// do something with those slots here
sebuckler
  • 56
  • 5