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...
A business has recently deployed laptops to all sales employees. The laptops will be used primarily from home offices and while traveling, and a high amount of wireless mobile use is expected.To protect the laptops while connected to untrusted wireless networks, which of the following would be the BEST method for reducing the risk of having the laptops compromised?
A. MAC filtering B. Virtualization C. OS hardening D. Application white-listing
Which of the following best describes a single sign on account?
A. A security measure that allows only one failed login attempt. B. An individual's unique identity required to access an organization's network. C. When you enter your name into one account and you are automatically signed into other accounts and services. D. An organization's shared login credentials for general network access.