Problem 3-0: RNG

To start unit 3, you will start with problem 3-0: RNG which generates “random” numbers.

Be sure to watch the video about “pipes” to understand how to output this file to a text file, rather than the terminal window.

Also, as there is no check50, you can compare your output with the output of the CS50 version

~cs50/unit3/rng n max s

where n is the numbers of numbers you want to generate, max is the maximum value, and s is the seed, which you will need to use to compare your results with theirs!

When you compete the assignment, please complete these questions and save to a word or a txt file. Then add this file to the same dropbox folder, you add to dropbox.

  • What is a “seed” to a random number generator (RNG)?
  • Why do we describe the numbers generated by an RNG as pseudorandom and not truly “random”?
  • At the command line, what do < and > do, respectively?
  • At the command line, what does | do?

http://cdn.cs50.net/ap/1516/problems/3/0/3-0.html

If you want an idea of how to use srand48 and drand48, I’ve posted some code with comments to give you hints on how to proceed.

#define _XOPEN_SOURCE
#define LIMIT 65536

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>

// change main function to use command line arguments
int main(void)
{

   // if (argc != 3 and argc !=4), print error message, return 1
   (TODO: put your code here)

   // declare n as an int, and set equal to atoi(argv[1])
   (TODO: put your code here)

   // declare max as an int, and set equal to atop(argv[2])
   (TODO: put your code here)

   int seed = 1;
   // if (argc == 4) set seed equal to atoi(argv[3])
   (TODO: put your code here)

   // seed the random number generator 
   srand48(seed); 

   // print a 'random' number n times (use a for loop for i=0 to i<n)
   (TODO: put your for loop here)
   {
      printf("%i\n",(int)(drand48() * max)); 
   }

return 0; 
}