Q4. Back to roulette.Here you will write two functions that simulate various bets in roulette. Review the slides from class to see the possible bets and their pay offs.This should give you practice in

Q4. Back to roulette.

Here you will write two functions that simulate various bets in roulette. Review the slides from class to see the possible bets and their pay offs.

This should give you practice in the following aspects of function writing:

  • Identify the parameters to a function and write a signature for the function
  • Use if/else statements to control the flow of code evaluation
  • Compare the efficiency of vectorized computations to for loops

Additionally, you will compare various betting strategies.

In class (see the slides), we looked at a function to simulate betting red in roulette. We reproduce this function here for your convenience.

betRed = function(numBets, betAmt = 1) { winLoss = sample(rep(c(-1, 1), c(20, 18)), size = numBets, replace = TRUE) netGain = sum(winLoss * betAmt) return(netGain) }

We compare the net gain of two types of betting strategies: 100 bets of $1 on red and 1 bet of $100 on red. To do this we repeated the betting process 10,000 times to see the kinds of net gains we might get with each strategy.

gain100B.1D = replicate(10000, betRed(numBets = 100, betAmt = 1)) gain1B.100D = replicate(10000, betRed(numBets = 1, betAmt = 100))

  1. We examined the distribution of the 10,000 outcomes from these betting strategies with a plot. Do you remember the density plot produced in class to compare the strategies, i.e., the distribution of wins for each strategy? Write a function that takes as arguments the two list of gains (like gain100B.1D and gain1B.100D above) that will produce a plot overlaying the two distributions. Make sure the plot looks nice!

You should be able to use the above plotting function to compare the net gains from the 2 strategies by running it as follows:

plotGains(gain1B.100D, gain100B.1D)

  1. A Wallet

When we play roulette we start with a particular sum of money, e.g., we may have $100 in our wallet. Then what’s in our wallet impacts how much and how long we can bet. For example, with $100 in our wallet we cannot place a $200 bet. Also, depending on our winnings, we may be able to place 200 $1 bets or we may run out of money before then.

For your first function, augment the betRed function to accept an additional parameter called wallet that contains the starting amount of money. Call this new function betRed2. Give wallet a default value so that it runs like the original betRed function if the default value of wallet is used.

The return value from this function should be the net gain. As before, this is the winnings less the losses. However, if at some point in your betting, you no longer have enough funds to place a bet then you can’t continue your betting.

NOTE: DO NOT USE A FOR LOOP IN THIS FUNCTION. Consider instead the cumsum() function and logical expressions.

Here are some tests that you can run to see if your code is working correctly (note: there are errors right now because the code has not been written!)

betRed2(betAmt = 100, numBets = 1, wallet = 50)

## Error in eval(expr, envir, enclos): could not find function “betRed2”

# returns 0 betRed2(betAmt = 100, numBets = 1, wallet = 100)

## Error in eval(expr, envir, enclos): could not find function “betRed2”

# returns either -100 or 100 test1 = replicate(10000, betRed2(betAmt = 1, numBets = 100, wallet = Inf))

## Error in FUN(X[[i]], …): could not find function “betRed2”


## Error in mean(test1): object ‘test1’ not found

# should be near -5.26 test2 = replicate(10000, betRed2(betAmt = 100, numBets = 100, wallet = 500))

## Error in FUN(X[[i]], …): could not find function “betRed2”


## Error in eval(expr, envir, enclos): could not find function “plotGains”

Calculate Your Essay Price
(550 words)

Approximate price: $22

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
The price is based on these factors:
Academic level
Number of pages
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more

Order your essay today and save 10% with the coupon code: best10