-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.c
55 lines (47 loc) · 1.16 KB
/
generate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* generate.c
*
* Computer Science 50
* Problem Set 3
*
* Generates pseudorandom numbers in [0,LIMIT), one per line.
*
* Usage: generate n [s]
*
* where n is number of pseudorandom numbers to print
* and s is an optional seed
*/
#define _XOPEN_SOURCE
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// constant
#define LIMIT 65536
int main(int argc, string argv[])
{
// main function when executed it asks for arguments to be passed
if (argc != 2 && argc != 3)
{
printf("Usage: generate n [s]\n");
return 1;
}
// checks whether the number of arguments is equal to 2 or 3
int n = atoi(argv[1]);
// converts the 2nd argument from string to integer. It is the number of random numbers to be generated
if (argc == 3)
{
srand48((long int) atoi(argv[2]));
}
else
{
srand48((long int) time(NULL));
}
// srand48 is an initialization function which sets the higher order 32 bits of the random number to the 3rd argument or seed value
for (int i = 0; i < n; i++)
{
printf("%i\n", (int) (drand48() * LIMIT));
}
// success
return 0;
}