Jump to content

Need help with C++ Random Number Generation


Scripts

Recommended Posts

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()

 

MY CODE

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

int newFunction(int count, double probability) {
   
   double random;
   int total = 0;
   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

1 //seed

3 //number of random numbers (count)

0.5 //value for probability

 

MY OUTPUT

Enter integer for seed: 1

Enter the count of numbers to be generated: 3

Enter the probability: 0.5

0.394383

Number of generated random numbers less than probability entered is 1

 

 

HIS OUTPUT

Enter integer for seed: 1

Enter the count of numbers to be generated: 3

Enter the probability: 0.5

0.159812

0.216901

Number of generated random numbers less than probability entered is 2

 

 

He gave us this as a skeleton for the code:

 

#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;
}

 

 

I don't have a clue as to what I'm doing wrong. He uses an online textbook where he inserts his "key code" then when we run our code it compares the outputs and determines if they are the same. We are using the same compiler, and I am almost positive we are to use rand(). Please, I really need help...Thank you

Link to comment
Share on other sites

1 hour ago, Eliot said:

His test case is probably wrong.

It wasn't wrong, he just did it a random ass way.....I finally got it figured out
 

#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;
}

I had to change the 

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

to

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

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...