mercredi 22 février 2017

Random Number Gen between 0 & 1 [C++]

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

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.




Aucun commentaire:

Enregistrer un commentaire