Which of the following code fragments gives a random double value between 2 and 5 inclusive? You can assume that the random number seed has been set and any needed definitions and initializations have been made.
a) 3.0*rand() + 2
b) 3.0*(RAND_MAX-rand())/RAND_MAX + 2
c) ((RAND_MAX-rand())/static_cast
2
d) (RAND_MAX-rand())/static_cast
e) rand()/static_cast
Only d) gives consistently correct, useful answers.
Explanation: Part a) The library function rand() returns an int, not a double, so
this is not correct. Part b) is syntactically correct but rand() returns an int
between 0 and RAND_MAX. On one compiler available to this writer RAND_MAX is
2,147,483,647 (the value of the largest possible int value, MAX_INT). In
3.0*(RAND_MAX-rand()) the subtraction is done then the multiplication.
You might also like to view...
The __________ uses the __________ to physically access a disk’s directory.
a. file system/IOCS b. IOCS/file system c. open command/access command d. access command/open command
Define the following terms: indent levels, indent markers, and tab selector. Name and briefly describe the four tab options (hint: One tab option is a Decimal-aligned tab).
What will be an ideal response?
As you type the search criteria in the search box, Windows narrows down the search results and displays them in the Start menu.
Answer the following statement true (T) or false (F)
You can control the number of digits displayed by the browser using thetoFixed()method.
Answer the following statement true (T) or false (F)