# Money bag alghorithm

## Recommended Posts

Hi all. I have to make one simple algorithm, but I'm still in doubt how to do it. It is about simple game. Say person A have 1.000 gold and he have to guess a number from 1 to 10. He puts 1,2 or 3 gold on table and make a guess. Winning numbers are from 6 to 10, and loosing numbers from 1 to 5. Person B imagine one number, write it down, person A make a guess. If person A wins, he double his bet and put won money in a bag (1 become 2, 2-4 and 3 -6). If he loose he just loose a money. Well the tricky part comes. How can I achieve that person A after spending all his money on gambling have exact sum in his bag (1.000 gold)? thanks in advance

##### Share on other sites
Quote:
 Original post by streamerHow can I achieve that person A after spending all his money on gambling have exact sum in his bag (1.000 gold)?

I'm not sure I understand this. Are you asking how you could ensure that no matter what betting the player does, he/she will always end up with 1,000 gold? Or are you asking how you can check if the player has won exactly 1,000 gold? Please explain the situation again, if you will. Thanks.

##### Share on other sites
Yes I want to ensure that no matter what betting the player does, he/she will always end up with exact sum he/she is started with (in this example 1,000 gold).
Person B will be actually a program that decides player's chances to win/lose.

##### Share on other sites
Your game description is confused. If I understand correctly, the world chooses a number between 1 and 10. The player bids a sum and attempts to guess the number. He gets double his initial bid if he guesses, and loses his bid otherwise.

Assuming an intelligent player, the optimal strategy for the opponent is random choice. When the player bids a sum, he therefore has a probability p(win) = 0.1 and a probability p(lose) = 0.9 of losing. Therefore, no matter what the player bids on, the expected amount of money gained for a bid of x is v(win) p(win) + v(lose) p(lose) = 2*x*0.1 + 0*0.9 = 0.2 x. If you consider enough repetitions to play the initial 1000, then the total expected gain would be 200. The variance decreases when the sums that the player bids are smaller.

In practice you cannot guarantee a gain larger than 0 (what if the player always loses?).

##### Share on other sites
I'll try to wrote it differently.
There is Player and CPU. Player has 1000 gold in pocket.
CPU guess number 1 or 2
Player places the bid 1,2 or 3 gold
Player guess the number
If player guessed the number he gets double of his bid otherwise he lose his money
If player won money he puts won money in the bag
Game ends when player have no money in his pocket.
Won money is in the bag (and he cannot play with that money)

How can I achieve that player on the end of game has 1000 gold in bag?

##### Share on other sites
If you consider the random optimal strategy ToohrVyk explains, the game is equivalent to a simplified version of roulette, and if it's random it's random.
What you ask for is a fixed, completely cheating roulette: you need to make up wins and losses arbitrarily to obtain the desired sum.
Unfortunately you need either prescience or a very constrained system, to know how many times and how much the player will gamble; at each point you need to guarantee that enough gambles, and of the appropriate value, will be made to compensate the apparently random results of previous rounds.
You could settle for more realistic objectives, like letting the player gamble freely and randomly, then forcing him to leave if he wins or loses too much, and subsequently make him get or lose some money with any excuse necessary to compensate wins or losses.
Of course an exact compensation leaving the character as wealthy as he started will be perceived as very artificial, and worse it would make the gambling completely pointless unless it has non-monetary consequences.

##### Share on other sites
The only way you can do this is to constrain the player into playing a fixed number of times. If you know the number of times he will play it's easy to design an algorithm that will always leave him even, but without that constraint there is no way of doing it.

##### Share on other sites
Quote:
 Original post by tok_juniorThe only way you can do this is to constrain the player into playing a fixed number of times. If you know the number of times he will play it's easy to design an algorithm that will always leave him even, but without that constraint there is no way of doing it.

Huh?

If I swear to play this game exactly five times, what's your proposed strategy? And how can you possibly account for the case where I lose each time?

The only way I can see to win such a game is if the player is allowed to go infinitely into debt. That way, putting the same amount of gold down each time will send you on a random walk that is infinitely-likely to get you back to the starting point at some undetermined point in the future (well, an infinity of them).

Of course, the condition that the game ends when the money runs out removes any chance of such a strategy existing. This is why the seemingly attractive 'double-or-quits' roulette martingale doesn't work (and has ruined so many poor suckers [rolleyes]).

In other words, no strategy exists, and the player can do whatever the hell they want, without it making any predictable difference.

##### Share on other sites
I Think what tok_junior is getting at, is that if you know the player will run exactly 10 rounds, than you
can do 5 random runs, then predict based on the first 5 exactly what the next 5 runs have to be to get the desired amount.
But only if you know the bet, meaning it cant be 1,2,3. it'd have to be a single fixed sum.

and by know what they have to be, i mean if you got 2 losses, you need 1 win. so it cheats and gives you the win.

##### Share on other sites
Not sure if this is what you're thinking of, but the Gamblers Problem can be solved using reinforcement learning. Some aspects of that problem look similar to what you are describing

##### Share on other sites
With random choice there is absolutely no way to guarantee how much money the player wins.

-me

##### Share on other sites
Seems to me that the only way to keep the player's resources constant is to not let them play. However, if you want to make sure that there is a limit on the total amount of money in the game, then give the CPU a fixed amount of money to begin with.

If you give the CPU 1000 also, then the game ends when the player has 0 and the CPU has 2000, or vice-versa.

This is the game, right? The CPU is gambling against the player? Otherwise, where does the money the player wins come from?

##### Share on other sites
I can think of two strategies to "try" and get the amount back to where the player started from.

The first is the old "doubling up" strategy. Every time you are below your starting amount, bet the sequence 1,2,4,8,16 etc. until you win. Net gain = 1 when you eventually do win. Not a very good strategy because once you start losing you have to keep gambling more and more of your stack, and there is often a max bet limit which is soon reaached.

The second is a more subtle variant of the first, which takes longer but you can manage your win amount easier and the stake grows a bit more slowly. Let's say you need to win X amount of cash. You have a bet list that has a number of entries which sum to X.

Say you want to win 10 quid (the universal currency of the future). Your bet list could be 1,2,3,4 say, or 1,1,1,1,1,1,1,1,1,1.

Now you make a bet. Bet the sum of the first and last entries in your list. The 1,2,3,4 case you would bet 1+4 = 5. The second case would be 1+1 = 2. Bet this amount. If you win, cross the first and last entries (what you just staked) off the list and repeat until the list is empty, you have won 10 quid.

If you lose, add the losing amount to the end of the list, and start again. So case (i) would be 1,2,3,4,5 for a new stake of 6. Case (ii) would be 1,1,1,1,1,1,1,1,1,1,2 for a new stake of 3. Again, rinse and repeat.

The beauty of this system is that you only need to win 1/3 of the time (plus the length of the original list/2) to win your desired amount.

The fatal flaw is that after a large number of losses (which is guaranteed eventually in a pure random game), you end up with the same situation as the doubling up strategy, you run out of quids or you break over the max stake limit.

Is that the sort of thing you are after?

EDIT: If anyone uses this strategy to break the bank at monte carlo plaease remember where you heard it first and send me some of the winnings.

##### Share on other sites
The Solution, as I see it.
1) Ask the player how many times they want to play (up-front)
Only Offer Even Numbers

2) Fix their stake for each game. (1,2,3.. whatever)

3) Create an array with one element for each game.
Fill it randomly with 1/2 "wins" and 1/2 "loses"

4) Each time they play, it doesn't matter what they "guess"
They Win or Lose based on solely the pre-determined Win/Lose array.

This way you CAN guarantee they always end up with the same amount.
but make it look "random"

##### Share on other sites
Interesting explanations [smile].
Unfortuneatly daftasbrush, I can't ask player, and also there is a memory problem for large steckes. What is player wants to bet 1mill?
I don't want to use it for cheating, or to use some "winnings combinatios".
This will be the part of one game, and I need for my program to return exact money amount to players. If I put it fully random, then the end amounts can vary +-10 or more percent. This can be very nerve-racking for players, mostly if they win smaller amount that they are started with.
This is one part of game that some country laws demands to be implemented (this will be one casino game), so the program need to solve that player when he entered a game with X amount of money, he win that amount back.

And the game rules are as I wrote:
"There is Player and CPU. Player has 1000 gold in pocket.
CPU guess number 1 or 2
Player places the bid 1,2 or 3 gold
Player guess the number
If player guessed the number he gets double of his bid otherwise he lose his money
If player won money he puts won money in the bag
Game ends when player have no money in his pocket.
Won money is in the bag (and he cannot play with that money)"

##### Share on other sites
I don't quite understand the purpose of a gambling machine if you can't lose money. That would make it so the house always takes a loss which is very bad for buisness (The key is to make sure your losses are lower then your gains so you make money!).

Assuming that the player can't lose any money then the only solution I can think of is to weight the random number generator so that the winning number has a greater probability of being chosen but even then a string of losses is still possible.

I don't think this is possible to guarantee without rigging the system to make sure it doesn't make the player lose too much.

Oh and it is the nerve racking feeling of losing money that makes people put more money into the machine to attempt to win their money back :\.

##### Share on other sites
Actually in some countries in Europe, law don't allow you to spent more than 1 euro on a single game. Smart people [smile] made 2 bet games, that didn't fall foul of the law, one bet of 1 euro and second of 9, 10 or even more. The trick is that you have credit and bank money. When you put your money in machine, money is represented as: credit X amount and bank 0. Then players get in some simple game that pumps money from credit into a bank. Every win put money into a bank money. Well that is the problem I wanted to solve. Actually it isn't gambling game at all, game just pump money from credit into a bank for the players that want to play on a bigger bet. It is some kind of pre-game. What I wanted to provide is that player get same amount of money on the end of game. Off course that money is now in the bank, not in credit, and player from that point can, if he want, to play with bigger bets.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628336
• Total Posts
2982156

• 9
• 24
• 9
• 9
• 13