Matlab
% Many games require the player to roll two dice. The number on each % die can vary from 1 to 6. % (a) Use the rand function in combination with a rounding function % to create a simulation of one roll of one die. % (b) Use your results from part (a) to create a simulation of the % value rolled with a second die. % (c) Add your two results to create a value representing the total % rolled during each turn. % (d) Use your program to determine the values rolled in a favorite % board game, or use the game shown in Figure P3.20.
@Kainui
Currently watching youtube. I think i am overcomplicating this problem.....
I am more stump on the math portion.
HOW TO DRAW RANDOM INTEGERS FROM {1,2,...,N} BASED ON THE \(\verb+rand()+\) FUNCTION. Draw the number of numbers you want. ``` randomnumbers = rand(2) ``` Since it's the uniform distribution, use this: \( X\sim \mathrm{Unif}(0,1) \Rightarrow 6X \sim \mathrm{Unif}(0,6)\). Therefore, multiply by 6 the randomnumbers: ``` randomnumbers6 = 5 * randomnumbers ``` Use `round` on \(\verb++\). You should use the \(\verb+ceiling+\) function: - any value from (0,1] -> 1 - any value from (1,2] -> 2 - any value from (2,3] -> 3 - etc. After these two operations, you obtained your die-results from rand() numbers.
The MATLAB function is called \(\verb+ceil(...)+\).
Try this: \(\verb+ceil(6* rand(2))+\). What is the result?
ans = 1 5 5 1
why are we using rand(2) though?
why not rand(3 or 4 or 5)
sorry , I thought you needed to compute generate several numbers at the same type. Try: ceil(6*rand()).
>> ceil(6*rand()) ans = 4
lots of times... you should see it works.. generating numbers from 1 to 6.
@reemii what?
I meant: execute "ceil(6*rand())" many times, it should convince yourself that it gives numbers from 1 to 6.. with a probably uniform distribution
okay I see.
as I'm sure you can see, `rand(2)` will generate a random 2x2 matrix rand(3) will generate a random 3x3 matrix rand(4) will generate a random 4x4 matrix ... ... ... rand(N) will generate a random NxN matrix since you want a single random number, you want a random 1x1 matrix. Matlab thinks of scalar numbers as a 1x1 matrix. So you can say `rand(1)` or just say `rand()` to ensure the random result is between 1 and 6, you'll have to do 2 steps step 1) multiply the result of rand() by 6. This is so you get a result in the targeted range you want. In code, it would look like this `6*rand()` step 2) Use the ceiling function to ensure you land on a whole number. In code, it looks like this `ceil(6*rand())`
Brb
did you end up solving the problem @raffle_snaffle ? : )
@jtvatsim no I didn't get b or c yet
Join our real-time social learning platform and learn together with your friends!