Question

The programming language has to be C. Do you remember rand()? Do you remember how it...

The programming language has to be C.

Do you remember rand()? Do you remember how it can use srand() for a seed? Did you know that rand uses that seed as the initial state of their random number generator. Did you know that a random number generator is really a deterministic sequence generator?

Yeah! So your next random number might be calculated using a recursive equation like:

rand_n = rand_(n-1) * coeffecient1 + coeffecient2

That is the random number produced is the previous random number times coeffecient1 plus coeffecient2.

Random number generators rely on unsigned integer overflow.

For this question, question2.c has already been started. Don't change the lines above // Don't change anything above this line or below // Don't change anything below this line.

Replace // Your code goes here with your code.

Based on what's already in question2.c provide the functions seedMyRand and myRand. Code in question2.c that has been already started:

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#define DEFAULT_SEED 1
#define DEFAULT_COEFFECIENT1 1234567891
#define DEFAULT_COEFFECIENT2 987654321
const uint64_t MODULUS_CONSTANT = 10;
const uint64_t DEFAULT_NUMBERS = 1;

uint64_t coeffecient1 = DEFAULT_COEFFECIENT1;
uint64_t coeffecient2 = DEFAULT_COEFFECIENT2;

// Don't change anything above this line

// Your code goes here

// Don't change anything below this line

uint64_t input_u64(uint64_t default_value) {
/* Read a u64 from the input, (user or file)
* using default_value instead if the user enters 0
*/
uint64_t input_value = 0;
int scanned = scanf("%llu", (long long unsigned int *) &input_value);
if (scanned != 1) {
abort();
}
if (input_value == 0) {
return default_value;
}
return input_value;
}

int main() {
printf("What is the seed?\n");
uint64_t input_seed = input_u64(DEFAULT_SEED);

printf("What is coeffecient1?\n");
uint64_t input_coeffecient1 = input_u64(DEFAULT_COEFFECIENT1);

printf("What is coeffecient2?\n");
uint64_t input_coeffecient2 = input_u64(DEFAULT_COEFFECIENT2);

printf("How many numbers?\n");
uint64_t numbers = input_u64(DEFAULT_NUMBERS);

seedMyRand(input_seed, input_coeffecient1, input_coeffecient2);

for (uint64_t number = 0 ; number < numbers; number++) {
printf("%llu\n", (unsigned long long) myRand() % MODULUS_CONSTANT);
}

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#define DEFAULT_SEED 1
#define DEFAULT_COEFFECIENT1 1234567891
#define DEFAULT_COEFFECIENT2 987654321
const uint64_t MODULUS_CONSTANT = 10;
const uint64_t DEFAULT_NUMBERS = 1;

uint64_t coeffecient1 = DEFAULT_COEFFECIENT1;
uint64_t coeffecient2 = DEFAULT_COEFFECIENT2;

// Don't change anything above this line

// Your code goes here
uint64_t seed = DEFAULT_SEED;

void seedMyRand(uint64_t input_seed, uint64_t input_coeffecient1, uint64_t input_coeffecient2) {
    coeffecient1 = input_coeffecient1;
    coeffecient2 = input_coeffecient2;
    seed = input_seed;
}

uint64_t myRand() {
    uint64_t x = seed * coeffecient1 + coeffecient2;
    seed = x;
    return x;
}

// Don't change anything below this line

uint64_t input_u64(uint64_t default_value) {
    /* Read a u64 from the input, (user or file)
    * using default_value instead if the user enters 0
    */
    uint64_t input_value = 0;
    int scanned = scanf("%llu", (long long unsigned int *) &input_value);
    if (scanned != 1) {
        abort();
    }
    if (input_value == 0) {
        return default_value;
    }
    return input_value;
}

int main() {
    printf("What is the seed?\n");
    uint64_t input_seed = input_u64(DEFAULT_SEED);

    printf("What is coeffecient1?\n");
    uint64_t input_coeffecient1 = input_u64(DEFAULT_COEFFECIENT1);

    printf("What is coeffecient2?\n");
    uint64_t input_coeffecient2 = input_u64(DEFAULT_COEFFECIENT2);

    printf("How many numbers?\n");
    uint64_t numbers = input_u64(DEFAULT_NUMBERS);

    seedMyRand(input_seed, input_coeffecient1, input_coeffecient2);

    for (uint64_t number = 0 ; number < numbers; number++) {
        printf("%llu\n", (unsigned long long) myRand() % MODULUS_CONSTANT);
    }

    return 0;
}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
The programming language has to be C. Do you remember rand()? Do you remember how it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • there is a function to create two random numbers between 1 and 25 and a function...

    there is a function to create two random numbers between 1 and 25 and a function to add them together. add the prototypes correctly, call them from main correctly and output the results. The three functions should be subtractNumbers, multiplyNumbers, and divideNumbers. Remember that division won't always yield an integer so be sure to take care of that issue within the function (don't change the variable declarations). Don't change the code that I've given you. Follow the same pattern. #include...

  • Problem: Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should ge...

    Problem: Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should generate a random number in the range of 1 through 2. If the random number is 1, the function should display “heads.” If the random number is 2, the function should display “tails.” Demonstrate the function in a program that asks the user how many times the coin should be tossed and then simulates the tossing of the coin that...

  • Need help adjusting this code in C. I'm not sure this one part of the code...

    Need help adjusting this code in C. I'm not sure this one part of the code is correct, it needs to generate n random addresses between 0 and (2^32)-1. So I set it to "address = rand() % 232;" but I'm not sure if that is correct. Can you please fix thanks. #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { unsigned int address; unsigned int pg_num; unsigned int offset; unsigned int i; unsigned int n = 1000000; double time_taken;...

  • //In this assignment, we use multiple threads to calculate the frequencies of the first digits //of...

    //In this assignment, we use multiple threads to calculate the frequencies of the first digits //of the numbers in an array of long integers #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <assert.h> //#define NUM_THREADS 2 #define DIGITS 10 #define MAX 100000000 unsigned long a[MAX]; struct thread_data { int thread_num; int i, j;                           //the staring and ending index unsigned long freq[DIGITS];           // results }; //initialize the array void init_array(unsigned...

  • 7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card...

    7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains a pair. b) Determinewhetherthehandcontainstwopairs. c) Determine whether the hand contains three of a kind (e.g., three jacks). d) Determinewhetherthehandcontainsfourofakind(e.g.,fouraces). e) Determine whether the hand contains a flush (i.e., all five cards of the same suit). f) Determine whether the hand contains a straight (i.e., five cards of...

  • (Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card...

    (Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains two pairs b) Determine whether the hand contains a full house (i.e., three of a kind with pair). c) Determinewhetherthehandcontainsastraight flush (i.e.,fivecardsofconsecutivefacevalues). d) Determine whether the hand contains a flush (i.e., five of the same suit) #include <stdio.h> #include <stdlib.h> #include <time.h> #define SUITS 4 #define FACES 13 #define CARDS...

  • This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a fun...

    This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a function and three overloaded operators. You don't need to change anything in the Multiplex.h file or the main.cpp, though if you want to change the names of the movies or concession stands set up in main, that's fine. Project Description: A Multiplex is a complex with multiple movie theater screens and a variety of concession stands. It includes two vectors: screenings holds pointers...

  • C Programming Language The code below matches the sample input/output but is marked wrong. Are there...

    C Programming Language The code below matches the sample input/output but is marked wrong. Are there other ways to do this problem? The focus is on recursion and functions. #include<stdio.h> int joCheck(unsigned long long int number) { if(number % 7 == 0 || number % 8 == 0) { return 1; } else return 0; } int main() { int cases = 0; scanf("%d", &cases); for(int i = 1; i<=cases; i++) { unsigned long long int number; scanf("%d", &number); if(joCheck(number)...

  • C Programming #include <stdio.h> #include <stdlib.h> #include <time.h> // These defines do a text repl...

    C Programming #include <stdio.h> #include <stdlib.h> #include <time.h> // These defines do a text replacement // everytime the string 'ROWS' and 'COLUMNS' // are found in this specific source file. // You can play with these values. #define ROWS 5 #define COLUMNS 5 /* ============= Tutorial on Graph format ============ You are given a randomly generated graph that looks of the form: 0 0 1 1 1 1 0 0 1 1 0 1 0 1 1 1 0 0...

  • Code that needs to be modified: #include #include #include int main() { int i,N,x; unsigned int seed; double R; printf("\nEnter number of iterations and seed"); printf("\n"); scanf(&#3...

    Code that needs to be modified: #include #include #include int main() { int i,N,x; unsigned int seed; double R; printf("\nEnter number of iterations and seed"); printf("\n"); scanf("%i %u", &N,&seed); srand(seed);    for(i=0;i { R=(double)rand()/RAND_MAX; if (R<0.5) x=x+1; else x=x-1; } printf("Final location is "); printf("%d",x); printf("\n"); } Question: Write a code that generates N pairs of random numbers. Call the first member of each pair x and the second member y. Count how many of the N pairs obey x^2+y^2<1. Call...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT