In: Computer Science
please answer part 2 . the code for part 1 is provided at the end.
Part 1
There is a pair of functions in stdlib.h (make sure to include it) that are used for generating pseudo-random numbers. These are "srand" and "rand". Examples are on pages 686 and 687. E.g., function "srand(3)" provides the seed value of 3 to the srand function. (3 is just a number, and probably isn't even a good number for this purpose, but that's a discussion for another time). The reason that we use a seed value is so that we can reproduce the series of random numbers. Thus, we call them pseudo-random numbers. Being able to reproduce them is important to verify and debug code.
Write a C program to generate a set of random numbers for a given seed value (call it lab8_part1.c). Prompt the user for the seed value, or 0 to quit. Then print 5 random values. When you run your program, try a few different seed values, then try one that you've already tried. Verify that you get the same sequence.
Part 2
Copy the lab8_part1.c program to a new file, lab8_part2.c, to use in this part.
We will generate random values, but they should be limited to 0, 1, 2, or 3. To do this, think of a way to map the random value to a small value; there are many ways to do this, however, the way you choose must be reproducible. That is, if it maps value X to the value 2, it should do that every time the value is X. Create a function that accomplishes random number generation and mapping: the function should return a single integer that is a random value of 0, 1, 2, or 3. It does not need any inputs. To verify that it works, have your program print about 20 from it. If your program gives you values like -1 or 4, you know you have a problem. Also, if it never generates one of the values (0, 1, 2, or 3), then there is a problem.
Then create an array of 4 ints. Initialize that array to 0 values. Have your program prompt the user for an integer number, read it in, then generate that many random values (0, 1, 2, or 3). Do not show the random values. Instead, count them, i.e., every time the function produces a 0, the integer at array position 0 should be incremented. Once all the random numbers have been processed, display the counts. Verify that the numbers make sense; if your program says that there were 4 zero values, 6 ones, 5 twos, and 3 threes, but it was supposed to generate 20 values, you know there is a problem because 4+6+5+3 = 18, not 20. Also have your program report the percentages as well as the counts. The percentages should be shown with one digit after the decimal, and they should add up to 100% (neglecting any round-off error).
Test your program a few times, and note the relative number of each generated value. Assuming an even distribution, you would see the same counts for each value, i.e. 0 would be generated 25% of the time, 1 would be 25% of the time, etc. The more values the program generates, the closer to 25% each one should be.
Prepare the log like you normally do: use "cat" to show the C programs, use "gcc" to compile them, and show that the programs run.
code for part 1:
CODE -
#include<stdio.h>
#include<stdlib.h>
int main()
{
int seed;
// Taking seed value as input from the user
printf("\nEnter a seed value (0 to quit): ");
scanf("%d", &seed);
// Running the loop until user enters 0 to quit
while(seed != 0)
{
// Passing seed value to the function srand()
srand(seed);
// Printing 5 random values
for(int i=0; i<5; i++)
printf("%d ", rand());
// Taking next seed value as input from the user
printf("\nEnter a seed value (0 to quit): ");
scanf("%d", &seed);
}
return 0;
}
Approach :
1.Generate a random number using random function.Lets called it rand_num
2. Take remainder of rand_num with 4 as any number % 4 will give result either 0,1,2,3
Eg. 20 % 4= 0
14 % 4 = 2
3. Declare a array count[4] and count frequency of each 0,1,2,3
4. Print count and percentage of each element 0,1,2,3
Below is commented code
#include<stdio.h>
#include<stdlib.h>
double percentage(double num, double total) {
double ans = (num / total) * 100.0;
return ans;
}
int main()
{
int seed;
// Taking seed value as input from the user
printf("Enter a seed value (0 to quit): \n");
scanf("%d", &seed);
// Running the loop until user enters 0 to quit
// count array will count frequency of 0 , 1 , 2 ,3
int count[4], total = 0;;
for (int i = 0; i < 4; i++) count[i] = 0;
while (seed != 0)
{ // Passing seed value to the function srand()
srand(seed);
// Printing 5 random values
for (int i = 0; i < 5; i++) {
// generae a random number and take it modulo with 4
int rand_num = rand() % 4;
printf("%d ", rand_num);
count[rand_num]++;
total++;
}
printf("\n");
// Taking next seed value as input from the user
printf("Enter a seed value (0 to quit): \n"); scanf("%d", &seed);
}
// print count of each element [0-3]
for (int i = 0; i < 4; i++) printf("Count of %d = %d\n", i , count[i]);
// Print percentage
for (int i = 0; i < 4; i++)
printf("Percentage of %d = %.1f\n", i, percentage(count[i], total));
return 0;
}
Below is screenshot of code along with output
Feel free to ask doubts in comments if any.