Functions in C Pseudorandom Number Generator

CS 2505 Computer Organization I
Functions in C
Assignment 4: Functions in C
Pseudorandom Number Generator
Assignment
For this assignment, you will implement several C functions that will generate pseudorandom numbers using the RANDU
algorithm. This algorithm was popular on mainframes in the past, but is no longer used since it's an extremely weak
(predictable) way to generate random numbers.
Given an odd seed number, a new "random" integer is created with this equation:
Vj+1 = 65539 * Vj mod 231
For example, if your seed number is 1, then you can produce a sequence of random numbers like so:
65539 = 65539 * 1 mod 231
393225 = 65539 * 65539 mod 231
…
Further, given a randomly generated integer, random numbers between 0 and 1 can be produced with a little extra effort:
Xj = Vj/231
You can read more about this algorithm and its weaknesses on Wikipedia:
http://en.wikipedia.org/wiki/RANDU
Implementation
You will provide an implementation of two C functions that perform the above calculations and have the following
interface:
/** Produces a random integer from "seed" using the RANDU algorithm.
* The RANDU algorithm expects the seed to always be odd, but our function
* has no such requirement. If an even seed is passed to your function,
* move it to the next largest odd number. So 4 would be come 5, etc.
*
* Example usage:
* x = randomInt(255);
// Initializes random number generator with seed 255
*
// and produces a random number.
*
// x = 16712445
* y = randomInt(255);
// Same seed, get the next random number in the sequence
*
// y = 100272375
* x = randomInt(16536); // New seed, reinitialize the random number generator
*
// x = 1083818443
*
* Restrictions (these apply to both functions):
* - You may not use global variables.
* - You must use at least one file local helper function in this assignment.
* - You may not use the math library when implementing this function. The
*
simplest way to compute 2^31 is to use the bitwise left shift operator.
*
uint32_t z = 1 << 31; // Equivalent to 2^31
*
* - You may need to cast the result of 1 << 31 to an unsigned integer.
* - Each function should produce an independent sequence of random numbers.
*/
uint32_t randomInt(uint32_t seed);
This is a purely individual assignment!
1
CS 2505 Computer Organization I
Assignment 4: Functions in C
/** Produces a random double between (0,1) from "seed" using the RANDU algorithm.
* Has similar restrictions to randomInt, see the .h file for more information.
*/
double randomDouble(uint32_t seed);
The restrictions stated in the header comment for the function are not enforced by the autograding system, but they will be
checked by a TA after the project is closed. Violating the restrictions will result in a score of 0.
Testing
You should begin by downloading the posted source files, Random.h, and driver.c from the course website. You
must create your own separate .c file, this file should be named Random.c and should contain only the implementation
of the functions described above, your helper function, and nothing else. To compile the program, you should use the
following command:
Linux> gcc –o Random –std=c99 –Wall driver.c Random.c
Once you've eliminated compile-time errors (and ideally warnings as well), you could execute the program by using the
following command, where the parameter 255 is a starting seed number:
Linux> ./Random 255
Next, you should compare the output your program writes to the corresponding posted output file. If there are differences,
analyze the reason for them and fix your code. Repeat as necessary.
You should not submit your solution to the Curator until you can correctly process all the posted test files. Even then, the
posted data files are not guaranteed to include all the possible logical cases, so you should consider writing additional test
files. It is perfectly OK to share test data files with other students, including on the course Forum board.
What to Submit
Submit your completed version of Random.c, after making changes and testing.
Your submission will be compiled, tested and graded according to the formatting of your output and how many cases your
solution handles correctly.
You will be allowed a limited number of submissions for this assignment (at least 10). Use them wisely. Test your
program thoroughly before submitting it. Make sure that your program produces correct results for every test case you can
think of. If you do not get a perfect score, analyze the problem carefully and test your fix with the test data shown in the
Curator grade report, before submitting again. The highest score you achieve will be counted.
The Student Guide and other pertinent information, such as the link to the proper submit page, can be found at:
http://www.cs.vt.edu/curator/
This is a purely individual assignment!
2
CS 2505 Computer Organization I
Assignment 4: Functions in C
Pledge:
Each of your program submissions must be pledged to conform to the Honor Code requirements for this course.
Specifically, you must include the following pledge statement in the submitted file:
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
On my honor:
- I have not discussed the C language code in my program with
anyone other than my instructor or the teaching assistants
assigned to this course.
- I have not used C language code obtained from another student,
or any other unauthorized source, either modified or unmodified.
- If any C language code or documentation used in my program
was obtained from an authorized source, such as a text book or
course notes, that has been clearly noted with a proper citation
in the comments of my program.
- I have not designed this program in such a way as to defeat or
interfere with the normal operation of the Curator System.
<Student Name>
Failure to include this pledge in a submission may result in a score of zero being assigned.
This is a purely individual assignment!
3