I'm new to Mathematica, and I would like to simulate the game of roulette.

I want to bet like \$1 on the opposite color every time four reds or four blacks come up. I would like to know if I will gain or lose money in the long run.

RandomChoice[{17/36, 17/36, 1/18} -> {red, black, green}]
  • 9
    $\begingroup$ You'll lose. No matter what "strategy" you pick... you'll lose. $\endgroup$ – bill s Dec 25 '15 at 3:27
  • 1
    $\begingroup$ Which one do you pick when green comes up 4 times in a row? $\endgroup$ – IPoiler Dec 25 '15 at 4:22

You go to the casino. You bet a dollar the ball will land on red. If it does you get two dollars if it does not you get zero. Likewise if you bet the ball will land on black. You cannot bet it will land on green.

Use this previous answer to help do the pattern matching. Note to the original poster, I found that using Google and searching for mathematica count subsequence.

g = RandomChoice[{17/36, 17/36, 1/18} -> {red, black, green}, 10^6];
win = {{red, red, red, red, black}, {black, black, black, black, red}};
loss = {{red, red, red, red, red}, {red, red, red, red, green},
  {black, black, black, black, black}, {black, black, black, black, green}};
ts = ToString@Row[#, ","] &;
{StringCount[ts@g, ts /@ win, Overlaps -> True],
 -StringCount[ts@g, ts /@ loss, Overlaps -> True]}

Whether you are considering overlapping patterns or not isn't specified, but this code is counting those too.

If I run that once I get {47043, -51943} which says I made 47043 winning bets and 51943 loosing bets in that particular 10^6 spins of the wheel.

Note to the original poster. Study this. Look up each function or thing you don't understand in the help system. Things like /@ and @ are also functions that you can find and try to understand. This is a complicated bit of code for someone who is new to Mathematica.

| improve this answer | |
  • $\begingroup$ This does not answer the OP (bets only on observation of 4-run). $\endgroup$ – ciao Dec 25 '15 at 9:02
  • 1
    $\begingroup$ @ciao I think this answers the OP. I think it counts every red 4-run followed by a black as a win, every 4-run of black followed by a red as a win and every other red or black 4-run as a loss. Perhaps I have made a mistake. If you could explain my error I would thank you and try to correct it. $\endgroup$ – Bill Dec 25 '15 at 18:03

Here's a q-d implementation:

With[{hits = 
   Cases[Partition[RandomChoice[{17/36, 17/36, 1/18} -> {1, 2, 3}, 1000000], 5, 1], {x_, x_, x_, x_, y_}]}, 
 Count[hits, {x_, x_, x_, x_, y_} /; y != x] - Length@hits]

It will run through 1 million spins, checking for any 4-runs and counting those where the subsequent spin differs, less the count in total (so +1 for "win", -1 for the bet itself).

You will see, as expected, you lose in the long run. Always.

| improve this answer | |
  • $\begingroup$ Of course, this presumes that Mathematica's random number generator is good for a million calls. $\endgroup$ – bill s Dec 25 '15 at 14:44
  • $\begingroup$ @ciao I think your answer counts red,red,red,red,green and black,black,black,black,green as wins. But the casino will not let you bet on green. That is how they make money. If you change your code to include y!=3 then this might work. $\endgroup$ – Bill Dec 25 '15 at 16:47
  • $\begingroup$ @ciao I think your answer counts a 4-run of green followed by anything but green as a win and I don't believe you can bet not-green so x!=3 seems required. With both these changes you and I seem to get the same numbers for the same 10^6 sequence. $\endgroup$ – Bill Dec 25 '15 at 18:00
  • $\begingroup$ @bill s Many years ago there was a spirited debate about the quality of the Wolfram random number generator algorithm. I don't remember all the details but I don't recall anyone being able to show clear flaws. $\endgroup$ – Bill Dec 25 '15 at 18:02
  • $\begingroup$ @bill s, "MersenneTwister" should do fine if it's only a few million or so… $\endgroup$ – J. M.'s discontentment Feb 17 '16 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.