Here are a few questions to help you clarify your understanding of random value generation techniques.
What standard library [header file] contains the definitions for the built-in random number generator? Explicitly what (three) functions/constants are missing if you fail to include this header?
The cstdlib contains the definitions for the [pseudo] random number generator. If we fail to include this header, RAND_MAX, srand, and rand are all missing.
The random number generator produces random values between 0 and a large constant (named RAND_MAX). In order to trim this huge range of values down to something we really need (like between 1 and 6 or 0 and 100), we use the power of integer arithmetic (the modulo (%) operator in particular). This allows us to reduce the large range to a smaller range which is more useful for our current application.
Show how you would create a random [integer] value between 1 and 6.
rand() % (6 - 1 + 1) + 1 rand() % 6 + 1
How about a random [integer] value between 0 and 100?
rand() % (100 - 0 + 1) rand() % 101
What is the general formula for generating random integral numbers between a given minimum and maximum?
rand() % (maximum - minimum + 1) + minimum
TRUE✓ | FALSE✗ | If you use the random number generator in your program, it will repeat the same sequence of values each time the program is run. | ||
---|---|---|---|---|
TRUE✗ | FALSE✓ | To avoid this we can use the start_rand() function to start the sequence at a different place each time the program is run. | ||
TRUE✗ | FALSE✓ | To find this different place, we use the seconds() function from the time library in a nested call to srand(). This function returns the number of seconds which have passed since midnight. | ||
TRUE✓ | FALSE✗ | The only odd thing [really] is that we have to place the required argument nullptr in our call to time(). | ||
TRUE✓ | FALSE✗ | One other thing to remember is to NOT place the srand() call just before the rand() call. This will cause the sequence to start over again before generating the next number. | ||
TRUE✓ | FALSE✗ | As fast as computers calculate, this could generate many restarts during the same second — causing all the random values to be the same. |
How might you generate random characters? Explain and/or code it here.
Generate random integers from to the desired range of ASCII codes and
cast them back to char.
static_cast<char>(rand() % ( static_cast<short>(maximum) -
static_cast<short>(minimum) + 1 )
+ static_cast<short>(minimum))
How might you generate random bool values? Explain and/or code it here.
We can generate random integers and cast them into bool values depending on the desired proportion of trues to falses: static_cast<bool>(rand() % n) // if n = 2, 50:50 true:false // if n = 3, 67:33 true:false // if n = 4, 75:25 true:false To flip the distribution proportion, just logically negate the result: ! static_cast<bool>(rand() % n) // if n = 2, 50:50 true:false // if n = 3, 33:67 true:false // if n = 4, 25:75 true:false But, to be as general as possible, select a random probability from the range of all probabilities ([0,1]) and compare this to your desired probability for a true result: rand() % RAND_MAX / (RAND_MAX - 1.0) <= prob
How might you generate random floating point values? Explain and/or code it here. (Be as general as you can!!!)
Taking the random value between 0 and 1 inclusive from the random bool answer, we can scale it out and shift it to produce any random floating point value: rand() % RAND_MAX / (RAND_MAX - 1.0) * (maximum - minimum) + minimum