Question

In: Computer Science

There is a pair of functions in stdlib.h (make sure to include it) that are used...

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.

Solutions

Expert Solution

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;

}

SCREENSHOTS -

CODE -

OUTPUT -

If you have any doubt regarding the solution, then do comment.
Do upvote.


Related Solutions

Make sure to include comments that explain all your steps (starts with #) Make sure to...
Make sure to include comments that explain all your steps (starts with #) Make sure to include comments that explain all your steps (starts with #) Write a program that prompts the user for a string (a sentence, a word list, single words etc.), counts the number of times each word appears and outputs the total word count and unique word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class:...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class: Introduction to C Programming 1057 Spring 2019 Section 004 * Assignment: Number 5 “estimate the value of a factorial using Gosper's algorithm." * Date: << 02-19-2019 >> * Version: 1 * Description: Program will prompt for some data, read in the values, perform the calculation using library math functions, and display the result. * File: lab5.c */ # define M_PI 3.14159265358979323846 /* pi */...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE *fp;        char c;        errno_t err;        err = 0;        err = fopen_s(&fp,"Desktop/test.txt", "r"); file is on my desktop but I err=2 but I don't think it is opening the file?        printf("%d\n", err);        if (err == 2)        {            printf("The file was opened\n");            while (1)       ...
Please Work on the commented parts in the code #include <stdio.h> #include <stdlib.h> /* * */...
Please Work on the commented parts in the code #include <stdio.h> #include <stdlib.h> /* * */ void printArray(int *arr, int size){ int i; for( i = 0; i < size; i++) { // Print each element out printf("%d ", *(arr+i)); //Print addresses of each element printf("%p", (arr+i)); //Printing a new line printf("\n"); } } int main() { // Allows user to specify the original array size, stored in variable n1. printf("Enter original array size: "); int n1 = 0; scanf("%d",...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h>...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 3 /* create thread argument struct for thr_func() */ typedef struct _thread_data_t {   int tid;   double stuff; } thread_data_t; /* thread function */ void *thr_func(void *arg) {   thread_data_t *data = (thread_data_t *)arg;   printf("hello from thr_func, thread id: %d\n", data->tid);   pthread_exit(NULL); } int main(int argc, char **argv) {   pthread_t thr[NUM_THREADS];   int i, rc;   thread_data_t thr_data[NUM_THREADS];   /* create threads */   for (i = 0;...
Includes you will need: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> Create a...
Includes you will need: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> Create a .c file that does the following: 1. Use fork() to create a 2nd process, using the if checks we’ve seen to determine if you’re the child or the parent. Print a message from each process saying who is who. (APUE chapter 1/7) • Note: The parent should save the pid_t of the child (returned from fork()) for later. 2. Have the child register a...
#include <chrono.h> #include <stdlib.h> #include <iostream.h> #include<conio.h> #include <iomanip.h> //#include <string.h> #include <fstream.h> //using namespace std;...
#include <chrono.h> #include <stdlib.h> #include <iostream.h> #include<conio.h> #include <iomanip.h> //#include <string.h> #include <fstream.h> //using namespace std; int main(int argc, char * argv[]) { ifstream myfile; myfile.open("Input.txt"); auto st=high_resolution_clock::now(); float sum = 0; float a=0; //myfile >> a; int i=0; while (!myfile.eof()) { myfile >> a; sum+=a; } myfile.close(); auto stp=high_resolution_clock::now(); auto dur=duration_cast<microseconds>(stp-st); cout<<"Total time taken ="<<dur.count()<<"ms"<<endl;// displaying time taken cout<<"Sum of integers: "<<sum<<endl; // displaying results getch(); return 0; } can someone please tell me what is wrong with this...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
I NEED THIS CODE FOR C++ IUSING SEMAPHORES PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ IUSING SEMAPHORES PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
summarize Symbolic Interactionism and make sure to include these point in it -NO social pressure...they are...
summarize Symbolic Interactionism and make sure to include these point in it -NO social pressure...they are in opposition to functionalism...they say you control you -Micro analysis...unlike the other two, symbolic interactionism now analyzes the individual and they interact with society or in a social institution -Symbols/definitions....people act towards some sort of social phenomenon based upon how they define it
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT