Problem 17 actuallyDue: February 9
Problem 17 asked you to write C++, Java, and Python programs to simulate one fair roll of a 6-sided die.
In Java and Python you can do this pretty simply by calling (respectively) the
nextInt(6) method from
java.util.Random or the
randrange(6) method from
But in C++, the problem is that if you just take a random 32-bit integer, and then mod it by 6, you introduce some bias where some numbers (specifically small ones) are more likely than others.
To remove this bias, you need to do something like the following:
def randomConvert(X, m1, m2): '''Given an integer X randomly chosen modulo m1, returns another integer randomly chosen modulo m2, or "FAIL". Note that m1 must be larger than m2! ''' c = floor(m1/m2) * m2 # c is the largest multiple of m2 that is less than m1. if X >= c: return "FAIL" else: return X % m2
So for this problem I want you to write a C++ program that basically imitates the functionality of Java and Python, as follows:
- Seed the C++ random number generator using
srand, where your random seed should either come from
/dev/urandomor by calling the
RDRANDinstruction directly as in Problem 16.
- Convert the value from
rand()into a fair 6-sided dice roll according to the general outline above. Note you might want to check out
RAND_MAXwhich will tell you what the largest possible output from
rand()is on your machine.
Submit your program according to the instructions on the submit page.