Matlab help
Watch this thread
Announcements
Page 1 of 1
Skip to page:
Haamid
Badges:
4
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#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.
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:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#2
Report
#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.
This is a basic programming task? What precisely do you need help with.
0
reply
Haamid
Badges:
4
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#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.
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
vicvic38
Badges:
19
Rep:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#4
Report
#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
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
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:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#5
Report
#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:
?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#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","yellow","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
function T=throwdice()
%Rolling a dice with numbers on one and colours on the other.
colours = (["red","blue","green","purple","yellow","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
Page 1 of 1
Skip to page:
Quick Reply
Back
to top
to top