Guest User

Untitled

a guest
Jul 15th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. The random number generator provided in C++ is really pseudo-random number generator. What this means is that if you examine the output you will find that the numbers repeat in a pattern. For this assignment I want you to create a better random number generator.
  2.  
  3. In object oriented programming what really want to do is reuse code. Since the rand function does a great deal of what we desire we can use it and simply add functionality to make it more efficient. The class you are going to be creating in this assignment is called Random and it will be loosely based on the Java Random class.
  4.  
  5. Here is the functionality
  6.  
  7. Constructor Summary
  8.  
  9. Random() - Default constructor
  10. Random(double min, double max); - generates random numbers between min and max.
  11. Random(Double min, Double max); - generates random numbers between min and max using class Double
  12. Random(int seed); - seed the random number generator
  13.  
  14. Function Summary
  15.  
  16. int nextInt() - Returns the next random number as an int
  17. Integer nextInteger() - Returns the next random number as an Integer class
  18. double nextDbl() - Returns the next random number as a primitive double
  19. Double nextDouble() - Returns the next random number as a Double class
  20. void setRange(double min, double max) - Sets the rage and recreates random numbers
  21. void setRange(Double min, Double max) - Sets the range and recreates the random numbers.
  22.  
  23. Specifics
  24.  
  25. Basically, this is a wrapper around the function rand. You will put a vector in your data section to hold 250 primitive double values. When one of the constructors is called you will clear the vector and fill it with new random doubles. For this you should have a private function called fillVect which will generate random doubles in whatever range is specified.. Once the vector is filled you will want to shuffle the values in the vector around. I will leave it up to you to determine how to shuffle the vector but it may be easy to simply swap values at two random indexes a bunch of times. This function should be called shuffle and should be defined as private.
  26.  
  27. To generate random doubles in a range you can use the following algorithm:
  28.  
  29. double r = (((double) rand() / (double) RAND_MAX) * (max - min)) + min ;
  30.  
  31. Where min and max are double values passed into the private function called fillVect. RAND_MAX is a constant that is added when you include iostream. You do not need to do anything to use it.
  32.  
  33. Constructors
  34.  
  35. The default constructor and the constructor that takes the seed should simply fill the vector with numbers in the range of 0 to RAND_MAX.
  36. All constructors except the constructor that takes the seed will use the time function to seed the random number generator.
  37. The constructor that takes seed should pass the value to srand for seeding.
  38.  
  39. Functions
  40.  
  41. The next functions should return the next value in the vector as whatever type specified.
  42.  
  43. Please note that when you have gone through 90% or more of the vector you should reshuffle and start from the beginning
  44.  
  45. The setRange functions should clear the vector and generate new random numbers for the vector. The vector should also be shuffled once it has been filled.
  46.  
  47. Finally, try to avoid using literal values. You know that you should use 250 numbers and you need to reshuffle at 90%. It would be best to use constants define these values.
Add Comment
Please, Sign In to add comment