Random Numbers

Hi there.

I am currently writing a basic fruit machine in C++. At the moment it's a simple text based machine that will spin in numbers (either 1, 2, or 3) and then reward the player with credits if they manage to spin in 1,1,1 - 2,2,2 - 3,3,3 etc.

I am using rand() to draw out the random numbers. With fruit machines, they don't usually pay out unless they have had a set amount of money put into them to counteract profit/loss (I assume anyway).
With the fruit machine I'm making, is there a way that I can limit the amount of times a sequence of numbers comes in?

For example;
If I wanted the sequence 1 1 1 to at least spin in within a random range of 15 spins. So within 15 spins, you are guaranteed to get 1 1 1 within 15 spins. And the same applies to the other sequences. But they will spin in less frequently as they will award more credits for the player to use. So, 2 2 2 to spin in within 30 spins, and 3 3 3, to spin in every 45 spins. (random examples).
Last edited on
First, you are learning, so don't learn the deprecated rand(). Learn the current tools for randomness. For example, the http://www.cplusplus.com/reference/random/uniform_int_distribution/


Second, there is no such thing as "guaranteed" in random. There is just probability. If you want to ensure that there is at least one win per 15 rolls, then you could on the 15th roll check whether the 14 previous rolls did win. If yes, do normal random roll. If not, then set 1-1-1 without randomness. Anyone testing the machine properly would probably notice and start to bet on the 15th rolls ...


You have three wheels. If one wheel has only three values, {1, 2, 3}, then the probability that one wheel gets specific value is 1/3 and that all three wheels get the same value is 1/3*1/3*1/3 = 1/27. The other combinations show up 24/27.

The wheels on those machines do have more than three values, do they not?

Lets make a machine with 6 values: {1, 1, 1, 2, 2, 3}. Now the chance to get '1' is 1/2, to get '2' is 1/3, and to get '3' is 1/6. That leads to:
all 1's: 1/8
all 2's: 1/27
all 3's: 1/216

Not quite the numbers you had in mind, but you can keep evolving the wheels.
Furthermore, each wheel can be different. The user will not know.

1
2
3
4
5
6
7
8
9
10
11
12
const std::vector<int> wheelA = {1, 1, 1, 2, 2, 3}

// construct a trivial random generator engine from a time-based seed:
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator( seed );

std::uniform_int_distribution<int> distribution( 0, wheelA.size() - 1 );


// one roll of the wheel:
int fruit = wheel[ distribution(generator) ];
if ( fruit == 42 ) bankruptcy(); // like we would lose. Ever. 
alternately you can roll 15 results at once ahead of time and store them, then randomly choose one (0-14) and assign that one a winning combo.



Registered users can post here. Sign in or register to post.