[H-1]
Write a function double randDouble()
that evaluates to a random double precision value d in the range 0.0 <= d
< 1.0
. Notice that the upper limit of 1.0 is NOT included in the range,
but the lower limit of 0.0 is in the range.
It is usual for random double generators to excluding the upper value, as here.
This corresponds to the interval that in mathematics is written
which means all values between zero and one, including the value zero, but excluding
the value one. This range works out best in many situations.[0, 1)
Hint: your function will use rand()
and RAND_MAX
.
Think carefully about any division you might do.
[E-8]
Test your function by writing a main()
that prints N random doubles, as here:
0.7571411133 0.7846984863 0.8890686035 0.1686096191 0.5905151367 0.9273681641 0.4562988281 0.7094421387 0.5547180176 0.3312988281 0.1849670410 0.2119750977 0.4522094727 0.0114746094 0.2628173828 0.9142150879 0.6651306152 0.0328063965 0.3609619141 0.7055969238 0.3167419434 0.1512145996 0.3352050781 0.1481323242 0.8327941895 0.2536621094 0.5308532715 0.6869201660 0.9351501465 0.5034790039 0.9721984863 0.2064819336 0.5976562500 0.5966491699 0.2182312012 0.5889282227 0.9966430664 0.0359497070 0.1654663086 0.4305114746 0.9865722656 0.8287658691 0.5792236328 0.5723571777 0.1323852539 0.5151367188 0.6348876953 0.6021118164 0.8298034668 0.9319458008
A small display such as the above would not show a problem, even if your generator is defective and sometimes produces an out-of-range value. A better test would be to generate several million values, testing that each one is in range.