COSC 1P03 Assignment 2 Life's full of randomness. But, like, not really Objective: To create implementations of an ADT, as well as test them. Background: Have you ever given much thought into how a...

1 answer below »
I need help with 2 more assignments. I can provide you a week before each assignment. it will be before July 7 all the assignments. the next 2 assignments will be of linked structures, generics, and recursion. the format will be the same as provided for the remaining 2 assignments. I need the same person to complete all three. as I have 3 assignments total please let me know the price of all three together . needs some discount too as I have 3 assignments to be completed


COSC 1P03 Assignment 2 Life's full of randomness. But, like, not really Objective: To create implementations of an ADT, as well as test them. Background: Have you ever given much thought into how a computer can generate “random” numbers? Remember, everything a computer does is deterministic by its very nature. Randomness doesn't exist in a computer (we're going to set aside the discussion over whether it exists at all). In spite of this, we often find ourselves needing random numbers. Or do we? Technically, we typically only need chaotic values: generated values, such that it's impractical to anticipate the next number in the sequence. So long as we have that, it'll suffice for most applications. And those applications are numerous: • Myriad forms of cryptography rely on them • Games can be less predictable • Multiple simulations of the same premises can have varying outcomes We typically achieve this via a PRNG — Pseudo-Random Number Generator A PRNG relies on three components: • Some internal state, that is updated with each number generated, such that successive calls will be different from each other • Some seed, to create the initial version of that internal state • A mathematical formula, of some sort, to create numbers based on that internal state (which also might be what updates it) An interesting side effect of how these operate is that, if you create two instances of the same type of PRNG, with the same starting seed, they always produce the same results. • In case you've ever wondered what a 'seed' is for procedurally-generated games: that's why • This can be handy if you ever need to re-play the results of something If you want to get different results, just use a different seed! • It's very common to incorporate the 'current time' into the seed, as later re-plays would presumably occur at different starting times A craptacular example: Let's design an absolutely terrible PRNG: • Our starting seed will always be the number 0 • Whenever we request a 'random' integer, we'll increment the state, and return that value • Floating-point values are more complicated, so we'll generate the next 'state integer', and then divide by some sufficiently large value to scale it to the range of [0..1) You can find an example of such here: https://www.cosc.brocku.ca/~efoxwell/private/1P03/2021s/CrappyRNG.java Note that this is not for this assignment. This just uses some notion of 'state' to generate values. https://www.cosc.brocku.ca/~efoxwell/private/1P03/2021s/CrappyRNG.java The actual task: We'll define a PRNG as such: • It is part of the random package • It is presumed to have some sort of starting seed ◦ We have the ability to request it (via getSeed) ◦ The seed should be of the long type ◦ If none is provided, use the current system time in milliseconds • It can yield a random integer ◦ The default is in the range of 0 to the maximum integer value ◦ You can request a different upper boundary, keeping 0 as the lower-bound ◦ You can specify both boundaries ◦ Ranges are subject to the following caveats: ▪ They are inclusive on the lower-bound, and exclusive on the upper-bound ▪ The upper-bound must always be higher than the lower-bound ▪ Negative values are possible, so long as they don't violate the above requirement ▪ Requesting invalid ranges throws a PRNGException • It can yield a floating-point (double) value, from 0.0 (inclusive) to 1.0 (exclusive) Of course, this means an interface. You'll also create three different implementations of this PRNG: • An incrementing PRNG that simply increments its internal state by one for each request ◦ Of course, this will not look very random! • A Timely PRNG that also keeps track of the passage of nanoseconds, as a source of entropy: ◦ Use the System.nanoTime function ◦ Unless a starting seed is explicitly provided for initialization, just use that 'nano time' ◦ Each time a value is generated, update its internal state based on the number of nanoseconds that have passed since the previous call • A basic (traditional) PRNG: ◦ Assuming some state (initially the seed), first generate an integer, and then take the modulus of that if you need to fit within a particular range ◦ For each such call, you also need to update that state: ▪ state=(1103515245×state+12345)%2147483648 ▪ There are other numbers you could use; these are just the classic C defaults ▪ (btw, 2147483648 is just Integer.MAX_VALUE, if you prefer. It's the highest positive value you can represent for a signed integer, i.e. 2³¹) Of course, all three implementations are part of the same (random) package. Finally, you'll write two client programs, both under the client package: • A very simple test harness, to demonstrate their usage ◦ You should write methods to test a PRNG, and then call them with different implementations ◦ Make sure to test that exceptions are thrown (do I need to say your client must catch them?) • A basic console-based game, that relies on randomness ◦ e.g. the “high-low” game: ▪ The game picks a random number from 1 to 100, and the user needs to guess it ▪ If the user guesses too high, it says to try 'lower'; and vice versa ▪ The user may repeat the game, etc. Just use your 'best performing' PRNG for the latter. Tips and special requirements: • There's seriously zero point in looking up how to do this online ◦ Besides the formula already being given to you, the generation itself isn't even really the point (this is the ADT assignment), and you'll just end up getting a zero if you try shoehorning online material into the assignment ◦ Please remember we have tutorials (and Help Desk) • You'll need at least two constructors per implementation: ◦ One that lets you assign an explicit seed, and one that uses the current system time ◦ You realize you shouldn't write these completely independently... right? ◦ Remember that, when calling getSeed, that yields the original seed, not the current state • For generating the floating-point value, you can just have it internally call its own nextInt() for some range, and then divide the result by that range (remember: double) • The seeds must be of the long type. That's actually a good thing: ◦ It makes the values easier to work with, because you're generating ints, but starting with longs. Values in the integer (32-bit) range won't overflow when stored in the long integer (64-bit) range ◦ The 'current system time' is already provided as a long value ◦ (Similarly, you'll want to maintain the 'internal state' as a long as well) ◦ Some marks will be dedicated to the 'appropriateness' of the implementation. As a hint of one such factor: you'll have three versions of nextInt, right? • This should be very clear by now, but you're required to use two separate packages: ◦ The testing code is not RNG code ◦ The RNG code is not for testing ◦ If you ignore this, please don't ask why you received a zero • You're still implementing this in Dr. Java, unless you make/made alternate arrangements in advance. This does not mean writing in something else, and then porting it over • You're still submitting a .zip file. Only. Not .rar. Not .7z. Again, assuming you don't want a zero Submission: Bundle up your solution, data files, and a sample execution (pretty much everything the marker needs to easily grade your work) into a .zip file, and submit it through Sakai. Some reminders: • .zip means .zip. Use a rar or 7z, and you'll get a zero • You're not writing your solution in one IDE and then converting to Dr. Java ◦ Unless you've made prior arrangements, you're using only Dr. Java, or you're getting zero (And yes, I realize I already said that above)
Answered Same DayJun 06, 2021

Answer To: COSC 1P03 Assignment 2 Life's full of randomness. But, like, not really Objective: To create...

Vibhav answered on Jun 06 2021
140 Votes
RandomGenerator/1.JPG
RandomGenerator/2.JPG
RandomGenerator/client/Client1.java
RandomGenerator/client/Client1.java
//package client;
//import random.*;
//
//public class Client1 {
//
//  public static void main(String[] args) {
//      System.out.println("PNRG version 1:\n");
//      PNRG1 rand
1 = new PNRG1();
//
//      System.out.println("5 Random numbers - ");
//      for(int i=0; i<5; i++) {
//          System.out.print(rand1.nextInt() + "   ");
//      }
//
//      System.out.println("\n5 Random numbers (0 to 21)- ");
//      for(int i=0; i<5; i++) {
//          try {
//          System.out.print(rand1.nextInt(21) + "   ");
//          }catch(PNRGException e){
//              System.out.println(e);
//          }
//      }
//
//      System.out.println("\n5 Random numbers (-21 to -17)- ");
//      for(int i=0; i<5; i++) {
//          try {
//          System.out.print(rand1.nextInt(-21, -17) + "   ");
//          }catch(PNRGException e){
//              System.out.println(e);
//          }
//      }
//      System.out.println("\nException example: ");
//      try {
//          System.out.print(rand1.nextInt(-5) + "  ");
//      }catch(PNRGException e) {
//          System.out.println(e);
//      }
//
//      System.out.println("\nPNRG version 2:");
//      PNRG2 rand2 = new PNRG2();
//
//      System.out.println("5 Random numbers - ");
//      for(int i=0; i<5; i++) {
//          System.out.print(rand2.nextInt() + "   ");
//      }
//
//      System.out.println("\n5 Random numbers (0 to 105)- ");
//      for(int i=0; i<5; i++) {
//          try {
//          System.out.print (rand2.nextInt(105) + "   ");
//
//          }catch(PNRGException e){
//              System.out.println(e);
//          }
//      }
//
//      System.out.println("\n5 Random numbers (20 - 300)- ");
//      for(int i=0; i<5; i++) {
//          try {
//          System.out.print (rand2.nextInt(20, 300) +  "   ");
//          }catch(PNRGException e){
//              System.out.println(e);
//          }
//      }
//      System.out.println("\nException example: ");
//      try {
//          System.out.print(rand2.nextInt(-15));
//      }catch(PNRGException e) {
//          System.out.println(e);
//      }
//
//      System.out.println("Exception example: ");
//      try {
//          System.out.print(rand1.nextInt(-5, -10));
//      }catch(PNRGException e) {
//          System.out.println(e);
//      }
//
//      System.out.println("\nPNRG version 3:");
//
//      PNRG3 rand3 = new PNRG3();
//
//      System.out.println("5 Random numbers - ");
//      for(int i=0; i<5; i++) {
//          System.out.print(rand3.nextInt() + "   ");
//      }
//
//      System.out.println("\n5 Random numbers (0 to 105)- ");
//      for(int i=0; i<5; i++) {
//          try {
//          System.out.print(rand3.nextInt(105) + "   ");
//
//          }catch(PNRGException e){
//         ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here