Question

Q1) A simple random generator is obtained by the formula rnew = (a×rold+b)%m and then setting...

Q1) A simple random generator is obtained by the formula rnew = (a×rold+b)%m and then setting rold to rnew. Write a program that asks the user to enter an initial value for rold. (Such a value is often called a seed). Then print the first 100 random integers generated by this formula, using a = 32310901, b = 1729, and m = 224 .

Q2) How thick does paper folding get? Take one sheet out of your newspaper, and fold it in half, then fold it in half again, and again, and again. Can you fold it 30 times? Pretending that you can (you probably can’t fold it more than 8 times), how thick would it be after 30 times? Assume the paper is 1/200 cm. thick. Write a program to solve this puzzle. Prompt for the number of folds and output the thickness in meters.

Note : Run by pycharm

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

Q1)

Solution:

Screenshot of the code:

Sample Output:

Code To Copy:

//Include the header file.

import java.util.Scanner;

class Main {

//Define the main function.

public static void main(String[] args) {

    //Create an object of Scanner type.

    Scanner sc = new Scanner(System.in);

    //Prompt the user to enter the value of r.

    System.out.print("Enter the value for r_old: ");

    int r_old = sc.nextInt();

    //Prompt the user to enter the value of a.

    System.out.print("Enter the value for a: ");

    int a = sc.nextInt();

    //Prompt the user to enter the value of b.

    System.out.print("Enter the value for b: ");

    int b = sc.nextInt();

    //Prompt the user to enter the value of m.

    System.out.print("Enter the value for m: ");

    int m = sc.nextInt();

   

//Declare an array to store 100 randomly

    //generated array.

    int[] numberArray = new int [100];

    //Begin the for loop.

    for (int i = 0; i < numberArray.length; i++) {

      //Compute the new value of r.

      int r_new = (a * r_old + b) % m;

      //Store it in the array.

      numberArray [i] = r_new;

      //Update the value of r_old.

      r_old = r_new;             

    }

    //Begin the for loop to diaplay the

    //value of randomly generated values.

    for(int i = 0; i < numberArray.length; i++){

      System.out.println(numberArray[i]);

     

      }

}

}

Add a comment
Know the answer?
Add Answer to:
Q1) A simple random generator is obtained by the formula rnew = (a×rold+b)%m and then setting...
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
  • Take one sheet out of your newspaper, and fold it in half, then fold it in...

    Take one sheet out of your newspaper, and fold it in half, then fold it in half again, and again, and again. Can you fold it 30 times? Pretending that you can (you probably can’t fold it more than 8 times), how thick would it be after 30 times? Assume the paper is 1/200 cm. thick. Write a program to solve this puzzle. Prompt for the number of folds and output the thickness in meters using python

  • (C++) Hey guys we just went over loops and it got me confused, it has me...

    (C++) Hey guys we just went over loops and it got me confused, it has me scrathcing my head for the past two days. I would appreciate any help you guys have to offer. Few places I have a hard time is get input and determine winner(cummulative part). Write a program that lets the user play the Rock, Paper, Scissors game against the computer. The computer first chooses randomly between rock, paper and scissors, but does not display its choice....

  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

  • Hi there! I need to compare two essay into 1 essay, and make it interesting and...

    Hi there! I need to compare two essay into 1 essay, and make it interesting and choose couple topics which im going to talk about in my essay FIRST ESSAY “Teaching New Worlds/New Words” bell hooks Like desire, language disrupts, refuses to be contained within boundaries. It speaks itself against our will, in words and thoughts that intrude, even violate the most private spaces of mind and body. It was in my first year of college that I read Adrienne...

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