Logic question

Question : The rand() function generates values on the basis of a distribution called the normal
distribution. One of the favorable properties of the normal distribution is that its average is exactly the
middle value of its range. It means that if we run the rand() function in C++, having the range 0 - 32767,
a large number of times and calculate the average of all the generated values, it would be exactly
16383.5. In this problem, you are required to find the smallest value of n (number of times the rand() is
called) that results in the average equal to 16383.5 +- 0.0001. Specifically, your program shall call rand()
1000 times and see the average, then call it another 10000 times and see the average, and so on until
the average is 16383.5 +- 0.0001.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    double x,y,c,n=1;
    while(n<=1000){
          x = (rand() % (32767 + 1 ) + 0) ;
          y = x/n;
          if((y = (16383.5+0.0001))||(y = (16383.5-0.0001))){
          break;
          }
          n++;
    }
    cout<<n<<endl<<y<<endl;
}


I just wanna ask how this code will pursue. In simple I want to understand a logic if someone of you got it better.
Last edited on
talal02 wrote:
The rand() function generates values on the basis of a distribution called the normal
distribution.

Where did you read that? As far as I'm aware, rand() tries (not entirely successfully), to generate a roughly uniform distribution of numbers. You may have confused this with the mean of a sample which, by the laws of large numbers (specifically, the Central Limit Theorem) would asymptotically tend to a normal distribution.



talal02 wrote:
having the range 0 - 32767

That's not universally true, either. It certainly isn't true on cpp.sh.



The fact that X=>Y does not imply Y=>X.

If you want to calculate an average then you ADD THINGS UP, then divide by the total number.

And your sample mean also has a variation (although not here, since you didn't call srand()).
Last edited on
I am not getting this question can you explain it a little bit
what are you not getting?
Did you understand The above, which explains that the question you were asked to code up is flawed, or you don't know how to do it. (Its doable even if flawed).

meh. maybe something like:
srand(time(0));
double avg = 0;
int ct = 0;
double total = 0;
while(fabs(avg - 16383.5) > 0.0001)
{
total += rand();
ct++;
avg = total/ct;
}
cout << ct;

this isn't perfect; at some point it will bork up due to total getting too large if you didn't find the right average. you can fix that by taking the average differently, with a little more code, if its an issue.
Last edited on
If you want floating point random numbers forget using the C library. It only generates integers. And doesn't do a good job of it. The C standard recommends not using rand().

https://web.archive.org/web/20180123103235/http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/

https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

The C++ <random> library can natively generate real numbers by using the std::uniform_real_distribution() object.

http://www.cplusplus.com/reference/random/uniform_real_distribution/

A tutorial on random number generation:
https://www.learncpp.com/cpp-tutorial/59-random-number-generation/
Topic archived. No new replies allowed.