-1

So my wonderful CS teacher has decided he's going to make our class revolve around ease for himself(instead of trying to make his students better at programming)....

BUT ANYWAYS....He gives us little info on how the program is to be written, and if we don't write it EXACTLY like he would, we don't get credit. I think I have written a working program(that does what he asks), but the hidden test cases keep failing so I could really use some help.

INSTRUCTIONS

  1. Write a function that takes an integer “count” and a double “probability"

  2. Generate “count” random numbers between 0.0 and 1.0

  3. Print the number of generated random numbers that is less than “probability”

  4. Call this new function from main()

CODE

#include <iostream>
#include <cstdlib>
using namespace std;

int newFunction(int count, double probability) {

   double random;
   int total;
   for(int i = 0; i < count; i++) {
      random = (double) rand() / RAND_MAX;
      if (random < probability) {
         cout << random << endl;
         total++;
      }
   }
   return total;
}

int main() {
  cout << "Enter integer for seed: " << endl;
  int seed;
  cin >> seed;
  srand(seed);

  int c;
  double p;
  cout << "Enter the count of numbers to be generated: " << endl;
  cin >> c;
  cout << "Enter the probability: " << endl;
  cin >> p;

  cout << "Number of generated random numbers less than probability entered is " << newFunction(c, p) << endl;

  return 0;
}

Program input is

1 //seed

3 //number of random numbers (count)

0.5 //value for probability

MY OUTPUT

Enter integer for seed:

Enter the count of numbers to be generated:

Enter the probability:

0.394383

Number of generated random numbers less than probability entered is 1

HIS OUTPUT

Enter integer for seed:

Enter the count of numbers to be generated:

Enter the probability:

0.159812

0.216901

Number of generated random numbers less than probability entered is 2

Default code given as template

#include <iostream>
#include <cstdlib>
using namespace std;

int newFunction(int count, double probability);

int main() {
  cout << "Enter integer for seed: " << endl;
  int seed;
  cin >> seed;
  srand(seed);

  int c;
  double p;
  cout << "Enter the count of numbers to be generated: ";
  cin >> c;
  cout << "Enter the probability: ";
  cin >> p;
  cout << "Number of generated random numbers less than probability entered is " << newFunction(c, p) << endl;

  return 0;
}

Maybe I am writing this incorrectly, but this issue with him giving us no credit for writing the code differently than he would have has happened before. I have taken many CS classes and this is the first time I have ever gotten 0 credit for a program that functions correctly/efficiently.

Any help would be great. Thanks guys.

baileyr12
  • 41
  • 6
  • You'll want to [watch this](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) if you're doing anything meaningful with random numbers in C++. If this depends on some particular implementation of `srand` it might be a lost cause unless you compile on exactly the same platform as your teacher. – tadman Feb 23 '17 at 01:03
  • "random numbers between 0.0 and 1.0"... [0., 1.), [0., 1.], (0., 1.], or (0., 1.)? – Dannyu NDos Feb 23 '17 at 01:07
  • Here's a good test case: "Enter integer for seed: FOOBAR!!!!!!!!" Try it and watch what happens. – user4581301 Feb 23 '17 at 01:10
  • Make sure you are using the same compiler (may also depend on the exact version) on the same system, otherwise you cannot expect to get the same numbers from `rand`, even with equal seeds. Using `rand` for an exercise like this is not a great idea to begin with, not lastly because of those portability issues. – Baum mit Augen Feb 23 '17 at 01:18
  • We are using the same compiler, it's an online textbook that has a built in compiler. It compares our code's output to his code's output, then tells us if we got the right output. He shows us one example if our output was correct, then hides the rest of the test cases(to prevent people from hardcoding outputs) – baileyr12 Feb 23 '17 at 01:20
  • 1
    Any idea what random number generator the teacher is using? No way you can match their results without it. – user4581301 Feb 23 '17 at 01:24
  • 1
    You forgot to initialize `total` (-> enable more compiler warnings), but other than that, your code looks like a correct solution (if we assume `rand` returns good random numbers). – Baum mit Augen Feb 23 '17 at 01:25
  • Of course, there is more than one equally correct way to map `[0, RAND_MAX]` to `[0,1]`, but if you have to guess the way the teacher did it, and he did something other than what we see above, the question is certainly ill-posed. – Baum mit Augen Feb 23 '17 at 01:28
  • Wow. I just ran the code though `-pedantic -Wall -Wextra -Wconversion` on and I didn't get a warning. GCC 4.9. VS 2015 spotted it. – user4581301 Feb 23 '17 at 01:30
  • @user4581301 Huh, that's somewhat troubling. Even gcc6.3 remains silent. Not even `-Wmaybe-uninitialized` and `-Wuninitialized` help. Clang does better: http://melpon.org/wandbox/permlink/B8U5kwS4f6UrNBMz – Baum mit Augen Feb 23 '17 at 01:32
  • Ah, got it: The warning occurs only with optimization enabled: http://melpon.org/wandbox/permlink/S1VPDDpkHUqdIQQv – Baum mit Augen Feb 23 '17 at 01:39
  • Teach me to run the debug build. As soon as I put an uninitialized variable in a loop, anonymous scoping braces aren't enough, unoptimized GCC just goes blind. Well, that's getting added to the self- code review checklist: do all pedantic code checks with the optimizer turned on. – user4581301 Feb 23 '17 at 01:46
  • Anyone have an idea how to make this work? We use the same compiler, pretty sure we are supposed to use rand(). He gave us a template of code to write into, and I will add that to the post if that will help....I'm just so frustrated – baileyr12 Feb 23 '17 at 02:21
  • Start by fixing the uninitialized variable @BaummitAugen spotted. Who knows what value `total` starts with? Hard to run a sum if you don't know the starting value. It still may not give you the same answers as the instructor expects, but it's one less problem. The template definitely suggests the instructor expects you to use `rand` and other than maybe tripping over some [floating point fuzz](http://stackoverflow.com/questions/588004/is-floating-point-math-broken), things look... Oh smurph. Lemme check something. – user4581301 Feb 23 '17 at 03:21
  • Nope false alarm. Math checks out. – user4581301 Feb 23 '17 at 03:23
  • the value total has been fixed, but yeah I still don't get the correct value... – baileyr12 Feb 23 '17 at 03:25
  • You can tell your teacher I said this is poor homework question if you want to. :) What you did is one correct solution. – Baum mit Augen Feb 23 '17 at 14:05
  • Check your lecture notes for whether your instructor gave explicit directions on how to convert `rand()` to a U(0,1). Is it possible he's dividing by `RAND_MAX + 1`, for instance? Pre-converting the divisor to a multiplicative scale constant? – pjs Feb 23 '17 at 15:00
  • I FINALLY GOT IT GUYS!! I'll post solution below if anyone is interested! – baileyr12 Feb 24 '17 at 03:48

1 Answers1

1

Fixed Code

#include <iostream>
#include <cstdlib>
using namespace std;

int newFunction(int count, double probability) {

   double random;
   int total = 0;
   for(int i = 1; i <= count; i++) {
      random = rand()/(double)(RAND_MAX + 1) + 1;
      if (random < probability) {
         cout << random << endl;
         total++;
      }
   }
   return total;
}

int main() {
  cout << "Enter integer for seed: " << endl;
  int seed;
  cin >> seed;
  srand(seed);

  int c;
  double p;
  cout << "Enter the count of numbers to be generated: " << endl;
  cin >> c;
  cout << "Enter the probability: " << endl;
  cin >> p;

  cout << "Number of generated random numbers less than probability entered is " << newFunction(c, p) << endl;

  return 0;
}

When I added 1 to RAND_MAX

random = rand()/(double)(RAND_MAX + 1);

I saw it was giving me a negative result...so I just changed it to

random = rand()/(double)(RAND_MAX + 1) + 1;

That solved the issue!

baileyr12
  • 41
  • 6