It is often useful to be able to simulate the throw of a fair die. Write a MATLA
ID: 1385981 • Letter: I
Question
It is often useful to be able to simulate the throw of a fair die. Write a MATLAB function, dice.m, that simulates the throw of a fair die by returning some random integer between 1 and 6 every time that it is called. (Hint: Call rand to generate a random number. Divide the possible values out of rand into six equal intervals and return the number of the interval that a given random value falls into.) Write a script, test_dice.m, that simulates 10,000 times with drawing a histogram figure and displays the first 30 values in the dice. The histogram must have title, x-label, and y-label. Show the function dice.m, the script test_dice.m, and the testing result and figure
Explanation / Answer
-*) function dice.m
X = rand; %returns a single uniformly distributed random number between 0 and 1.
function dice = Drop_dice(X)
%this function returns a single dice value
%between 0 and 6.
for i=0:1
cal = X*6; %returns a single uniformly distributed random number between 0 and 6.
dice = floor(cal); %returns a random integer number between 0 and 1.
end
fprintf(1,'%f ',dice);
end
-*) the script test_dice.m
X = rand; %returns a single uniformly distributed random number between 0 and 1.
while n<10000
function dice = Drop_dice(X)
%this function returns a single dice value
%between 0 and 6.
for i=0:1
cal = X*6; %returns a single uniformly distributed random number between 0 and 6.
dice = floor(cal); %returns a random integer number between 0 and 1.
end
fprintf(1,'%f ',dice);
end
n = n + 1;
end
-*) optional dice.java
import java.util.Random;
public class dice {
public static void main(String[] args){
int n=0, i, dice;
try{//Parse the string argument into an integer value
n=Integer.parseInt(args[0]);
}
catch(java.lang.ArrayIndexOutOfBoundsException e){
System.err.println("One integer: Number of Dices");
System.exit(1);
}
Random generator = new Random();
// Random generator = new Random(puedes poner la semilla a mano);
for(i=0; i<n; i++){
dice=generator.nextInt(6)+1;
//print the rest of the random numbers
System.out.print(" " + dice);
}
System.out.print(" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.