You are permitted, and encouraged, to work together on this problem,
provided that every contributor understands how the complete solution
was found. Put the names of all contributors into comments at the top of
each file:
/* assignment 13 */
/* Joe Bloggs */
/* Alice Smith */
Your solution is due Friday 30 November at 17:00. Send each file in a
separate email message to djb-275-hw@cr.yp.to:
mail djb-275-hw@cr.yp.to < randomfloat.h
mail djb-275-hw@cr.yp.to < randomfloat.c
13. Write a randomfloat library, consisting of two files, randomfloat.h
and randomfloat.c. Other programmers will be able to write code such as
#include "randomfloat.h"
main()
{
...
randomfloat_init(n);
f1 = randomfloat();
f2 = randomfloat();
...
}
to produce random numbers using your library. The randomfloat_init() and
randomfloat() functions are similar to the standard srand() and rand()
functions, but produce real numbers between 0 and 1 instead of integers.
Those real numbers are defined as follows:
* Start from n, the argument to randomfloat_init(), an integer
between 0 and 4294967295. (2 to the 32nd power is 4294967296.)
* Define x{0} as n divided by 4294967296. For example, if n is
1048576, then x{0} is 0.000244140625.
* Define x{2} as 1. Define x{k} as 0 if k is any other number between
1 and 54.
* For k at least 55: Define x{k} as x{k-24}+x{k-55} if that sum is
smaller than 1, or x{k-24}+x{k-55}-1 otherwise.
* The value returned by the first call to randomfloat(), f1 in the
above example, is x{1000}; the value returned by the second call to
randomfloat() is x{1001}; and so on. For example, if n is 1048576,
then the first few values are 0.78515625, 0, 0.943359375,
0.1943359375, 0, 0.16259765625, and 0.2548828125.
The programmer may use randomfloat() without randomfloat_init(). In this
case, randomfloat() must automatically call randomfloat_init(n) where n
is the number returned by the standard time() function.
Hints: You do not need to use dynamic memory allocation. Put the number
x{k} into array element x[k % 55].
(Medium-quality random number generators are discussed in some courses
on numerical analysis. High-quality random number generators are
discussed in courses on cryptography.)