MATLAB: How to make a rand function generate numbers between 0.527 and 1.527? And how to make sure the problem runs for 10,000 rounds

randomrandom number generator

My question for MATLAB is: A casino offers the following two similar games:
Game A:
You make your bet and start with one 1 point. The casino generates a random number between 0.527 and 1.527, and multiplies your points by that amount. The game ends after 10,000 rounds, after which, the casino pays you twice your bet if you have more than 1 point. Otherwise, you lose your bet.
Game B:
The same as game A except a random number is generated between 0.547 and 1.547.
You should adapt the previous examples of for-loops in order to code your own for-loop to answer the following question. Note that the rand function generates a random number between 0 and 1, hence
>> rand – 0.5
generates a random number between -0.5 and 0.5
What game(s) should you play to beat the casino?
I am confused because when I input rand – 0.527 it creates numbers less than .527. Would I need to input rand – 1.527?

Best Answer

Think about what you are doing, about what happens. Take your time. Think. Play with it. Start with rand. It produces numbers where, over what interval?
Hint: [0,1] (Ok, the endpoints don't get hit by rand as I recall.)
Now, if you want the lower endpoint to be 0.527, what would happen if you ADDED 0.527 to the result of rand? If the lower endpoint is originally 0, and you want the lower endpoint to be some number a, then add a to rand.
What would that do to the upper endpoint?
TRY IT!!!!!!!!!!!!
What does this show you?
x = rand(1,1e6) + 0.527);
min(x)
max(x)
In fact, if you read the help docs for rand, you will find exactly this information. See here:
Example 1: Generate values from the uniform distribution on the
interval (a, b).
r = a + (b-a).*rand(100,1);
Related Question