Haamid
Badges: 4
Rep:
?
#1
Report Thread starter 11 months ago
#1
how do i do this

Exercise 2.9 (Hard) A board game is played with two dice. One of the dice has its faces numbered from 1 to 6, the other dice has a different colour in each of its faces: red, blue, green, yellow, orange and purple. Write a MATLAB function named throwdice.m that simulates the two dice being thrown and returns one number and a colour. Hint: you can use the MATLAB built-in function randi.
0
reply
vicvic38
Badges: 19
Rep:
?
#2
Report 11 months ago
#2
You could do it with a bunch of if-else statements. Call randi to generate two numbers between 1 and 6, and then for one of the die, assign a colour to each number using the if elses, then just return the number of the die and a colour.

This is a basic programming task? What precisely do you need help with.
0
reply
Haamid
Badges: 4
Rep:
?
#3
Report Thread starter 11 months ago
#3
(Original post by vicvic38)
You could do it with a bunch of if-else statements. Call randi to generate two numbers between 1 and 6, and then for one of the die, assign a colour to each number using the if elses, then just return the number of the die and a colour.

This is a basic programming task? What precisely do you need help with.
ive got it so i can get a random number but i cant get it to chose a random colour, how do i exactly do that, and also this is my first time using matlab as well
0
reply
vicvic38
Badges: 19
Rep:
?
#4
Report 11 months ago
#4
(Original post by Haamid)
ive got it so i can get a random number but i cant get it to chose a random colour, how do i exactly do that
You use if else statements to assign the result of Randi to a colour. Something like the pseudocode here:
IF die2 = 1 THEN:
colour = red
ELSEIF die2 = 2 THEN:
colour = blue
ELSEIF die2 = 3 THEN:
colour = green
ELSEIF die2 = 4 THEN:
colour = yellow
ELSEIF die2 = 5 THEN:
colour = orange
ELSE:
colour = purple
END

I'm sure you could do it with a matrix? You could also define a vector whose elements were the colours, and select them using the Randi result:
Colours = [red, blue, green, yellow, orange, purple]
colour = Colours(die2)
0
reply
Shimo
Badges: 16
Rep:
?
#5
Report 11 months ago
#5
I would set up an array for the colours, e.g. colours = [red, blue, green], that way you don't have to have a bunch of if statements
0
reply
Haamid
Badges: 4
Rep:
?
#6
Report Thread starter 11 months ago
#6
Finally did it, with a simple version. this is the input script

function T=throwdice()
%Rolling a dice with numbers on one and colours on the other.
colours = (["red","blue","green","purple","y ellow","orange"]);
die2 = randi(6);
colour = colours(die2);

disp(['The random number is ', num2str(randi(6,1)), ' and the random color is ', num2str(colour)])


final reuslt is
>> throwdice()
The random number is 2 and the random color is red
>> throwdice()
The random number is 1 and the random color is green
>> throwdice()
The random number is 3 and the random color is purple


thanks for your help guys
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Have you experienced financial difficulties as a student due to Covid-19?

Yes, I have really struggled financially (58)
17.31%
I have experienced some financial difficulties (94)
28.06%
I haven't experienced any financial difficulties and things have stayed the same (128)
38.21%
I have had better financial opportunities as a result of the pandemic (44)
13.13%
I've had another experience (let us know in the thread!) (11)
3.28%

Watched Threads

View All