Usage of arrays to generate random numbers - c++

I'm trying to generate N random floats between 0 and 1 where N is specified by the user. Then I need to find the mean and the variance of the generated numbers. Struggling with finding the variance.
Already tried using variables instead of an array but have changed my code to allow for arrays instead.
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int main(){
int N, i;
float random_numbers[i], sum, mean, variance, r;
cout << "Enter an N value" << endl;
cin >> N;
sum = 0;
variance = 0;
for (i = 0; i < N; i++) {
srand(i + 1);
random_numbers[i] = ((float) rand() / float(RAND_MAX));
sum += random_numbers[i];
cout << random_numbers[i] << endl;
mean= sum / N;
variance += pow(random_numbers[i]-mean,2);
}
variance = variance / N;
cout << " The sum of random numbers is " << sum << endl;
cout << " The mean is " << mean << endl;
cout << " The variance is " << variance << endl;
}
The mean and sum is currently correct however the variance is not.

The mean you calculate inside the loop is a "running-mean", ie for each new incoming number you calculate the mean up to this point. For the variance however your forumla is incorrect. This:
variance += pow(random_numbers[i]-mean,2);
would be correct if mean was the final value, but as it is the running mean the result for variance is incorrect. You basically have two options. Either you use the correct formula (search for "variance single pass algorithm" or "running variance") or you first calculate the mean and then set up a second loop to calculate the variance (for this case your formula is correct).
Note that the single pass algorithm for variance is numerically not as stable as using two loops, so if you can afford it memory and performance-wise you should prefer the algorithm using two passes.
PS: there are other issues with your code, but I concentrated on your main question.

The mean that you use inside the variance computation is only the mean of the first to i element. You should compute the mean of the sample first, then do another loop to compute the variance.
Enjoy

Related

C++ 2 dice rolling 10 million times BEGINNER

I am trying to create a program that will roll 2 dice 10 million times, and output how many times each number is rolled. Along with this, I am tasked with creating a histogram (*=2000) for the outputs.
Here is what I have so far.
/*
Creating a program that counts outcomes of two dice rolls, then show a
histogram of the outcomes.
Section 1 : Simulate ten million times rolls of two dice, while counting
outcomes. (Hint: Use an array of size 13.)
Section 2 : Show the outcome, the numbers of outcomes, and the histogram
(one * designates 20000). Your output must align properly.
*/
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
int i, j, ary[13] = {};
cout << "Please enter the random number seed.";
cin >> j;
srand(j);
for (i = 0; i < 10000000; i++)
ary[die() + die()]++;
for (i = 2; i <= 12; i++)
{
cout << setw(3) << i << " : " << setw(6) << ary[i] << " : ";
for (j = 0; j < ary[i]; j += 2000)
cout << "*";
cout << endl;
}
return 0;
}
EXAMPLE OUTPUT: https://imgur.com/a/tETCj4O
I know I need to do something with rand() % 6 + 1; in the beginning of the program. I feel like I am close to being complete but missing key points! I also realize I have not defnied die() in my ary[]
I recommend creating random seeds from high precision timers such as std::chrono::high_resolution_clock. Then they are not dependent on the user and are actually random. Create the seed always before calling std::rand.
#include <chrono>
auto time = std::chrono::high_resolution_clock::now();
auto seed = std::chrono::duration_cast<std::chrono::milliseconds>(time);
std::srand(seed)
Millisecond precision makes the seed usually unique enough but if the seed is required close to 1000 times a second then i recommend using nanosecond or microsecond precision to be really random.
Best would be to create a function that creates the random seed using high precision timer and the random value and finally makes sure the return value is between 0 and 5 (for 6 sided dice).

Having garbage numbers while calculating percentage

So, I hate to ask, but, I'm having some issue with this, I'm new to C++ and I'm just starting out. Everything is done for the most part. Expect for a little thing.
Line 35-36 should be calculating the average (Which for some reason, I haven't been able to get it to work.)
Line 41-47 should print out the percentage that heads/tails was landed on with precision to one decimal, and then print out the correct numbers of * to represent the percentage.
But, when I run it, my heads/tail count is messed up. As well as my percentage numbers. I'm just looking for a push in the right direction.
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <iomanip>
using std::cout; using std::cin; using std::endl;
using std::fixed; using std::setprecision;
int main()
{
srand(time(0));
int userInput,
toss,
headsCount,
tailsCount;
double headsPercent = 0,
tailsPercent = 0;
cout << "How many times do you want to toss the coin? ";
cin >> userInput;
while(userInput < 0)
{
cout << "Please enter a positive number: ";
cin >> userInput;
}
for(int i = 1; i < userInput; i++)
{
toss = rand() % 2;
if(toss == 0)
headsCount++;
else
tailsCount++;
}
headsPercent = userInput / headsCount * 100;
tailsPercent = userInput / tailsCount;
cout << "Heads: " << headsCount << endl
<< "Tails: " << tailsCount << endl << endl;
cout << "Heads Percentage: " << fixed << setprecision(1) << headsPercent << " ";
for(int b = 0; b < headsPercent; b++)
cout << "*";
cout << "\nTails Percentage: " << tailsPercent << " ";
for(int b = 0; b < tailsPercent; b++)
cout << "*";
return 0;
}
In addition to the uninitialized variables here, that others have pointed out, the calculations are all wrong.
Take out paper and pencil, and run some your own calculations the old-fashioned way.
Let's say there were five tosses, three heads, two tails. This means that (after fixing the uninitialized variables):
userInput=5
headsCount=3
tailsCount=2
Now, here's how you're calculating your supposed percentages:
headsPercent = userInput / headsCount * 100;
tailsPercent = userInput / tailsCount;
So, using your own numbers, you will get:
headsPercent = 5 / 3 * 100
tailsPercent = 5 / 2;
Does this look right to you? Of course not. You can do the arithmetic yourself. Divide 5 by 3 and multiply by 100. This is integer division, so five divided by three is 1, multiplied by 100 is 100. Five divided by two is two. So you get 100% and 2% here.
Of course, that's wrong. Two and three times, out of five, is 40% and 60%, respectively.
Writing a program means:
A) Figure out how calculations need to be made
B) Write the code to do the calculations.
You're still on step A. You need to figure out how you want to make these calculations so they're correct, first.
This has nothing really to do with C++. If you were using any other language, and coded this, in that manner, you'll get the same wrong answers.
The only thing this might have to do with C++ is that integer division, in C++ does not produce a fractional amount. It's integer division. But that's not your only problem.
Firstly u have to correct ur basics of mathematics.
Calculating %age means
example
(Marks obtained)/(Total marks)*100
Not (Total marks/marks obt)*100
Dividing any no by 0 is not defined. So if ur current code randomly assign toss or head =0, then obviously u will have errors.
Secondly talking about codes, U should either initialize i from 0 , or u should use
for (i=1; i<=userInput; i++)
As otherwise the head+toss value will be userInput-1.
Also remember to initialise variables like
Int headsCount=0;
etc. As the variable will take any random value if not initialised to a fixed no. (Though it does not creates a problem here)
And just change the datatype
int userInput,
toss,
headsCount,
tailsCount;
To
double userInput,
toss,
headsCount,
tailsCount;
This will solve your problem.
Advice: Please use
using namespace std;
in the starting of ur programs as u have to type a lot of std::
Welcome to C++. You need to initialise your variables. Your compiler should have warned you that you were using a variable without initialising it. When you don't initialise a value, your program has undefined behaviour.
I'm talking about headsCount and tailsCount. Something like this should be fine:
int headsCount = 0, tailsCount = 0;
Also note that your loop should start at 0, not 1, since you are using the < operator on the final condition.
Finally, your percentage calculations are backwards. It should be:
headsPercent = headsCount * 100 / userInput;
tailsPercent = tailsCount * 100 / userInput;
Now, there's a weird thing that might happen because you are using integer division. That is, your percentages might not add up to 100. What's happening here is integer truncation. Note that I dealt with some of this implicitly using the 100x scale first.
Or, since the percentages themselves are double, you can force the calculation to be double by casting one of the operands, thus avoiding integer truncation:
headsPercent = static_cast<double>(headsCount) / userInput * 100;
In fact, since the only two possibilities are heads and tails, you only need to count one of them. Then you can do:
tailsPercent = 100 - headsPercent;
1) This loop should start from 0:
for(int i = 1; i < userInput; i++)
2) The divisions are not correct:
//headsPercent = userInput / headsCount * 100;
//tailsPercent = userInput / tailsCount;
headsPercent = headsCount / userInput * 100;
tailsPercent = tailsCount / userInput * 100;
3) Finally:
cout << "\nTails Percentage: " << fixed << setprecision(1) << tailsPercent << " ";

C++ source code bug -- Computing Differences in Distance and Total Sums

The purpose of this program is to be able to input a set of integer double values, and for it to output the total distance as a sum. It's also meant to recognize the smallest and largest distances -- as well as calculate the mean of two or more distances.
I would also like to be able to remove the repetitive block of code in my program, which I've literally copied to get the second part of the source code working. Apparently there's a way to remove the replication -- but I don't know how.
Here's the source:
/* These includes are all part of a custom header designed
by Bjarne Stroustrup as part of Programming: Principles and Practice
using c++
*/
#include<iostream>
#include<iomanip>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<list>
#include <forward_list>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include <array>
#include <regex>
#include<random>
#include<stdexcept>
// I am also using the "stdafx.h" header.
// reading a sequence of integer doubles into a vector.
This could be the distance between two areas with different paths
int main()
{
vector<double> dist; // vector, double integer value
double sum = 0; // sum of two doubles
double min = 0; // min dist
double max = 0; // max dist
cout << "Please enter a sequence of integer doubles (representing distances): \n";
double val = 0;
while (cin >> val)
{
if (val <= 0)
{
if (dist.size() == 0)
error("no distances");
cout << "The total distance is: " << sum << "\n";
cout << "The smallest distance is: " << min << "\n";
cout << "The greatest distance is: " << max << "\n";
cout << "The average (mean) distance is: " << sum / dist.size() << "\n";
keep_window_open();
return 0;
}
dist.push_back(val); // stores vector value
// updating the runtime values
sum += val;
if (val > min)
min = val;
if (max < val)
max = val;
}
if (dist.size() == 0)
error("no distances");
cout << "The total distance is: " << sum << "\n";
cout << "The smallest distance is: " << min << "\n";
cout << "The greatest distance is: " << max << "\n";
cout << "The average (mean) distance is: " << sum / dist.size() << "\n";
keep_window_open();
}
Additionally, I have been trying to input a small block of source code in the form of something like "catch (runtime_error e)" but it expects a declaration of some sort and I don't know how to get it to compile without errors.
Help with removing the replicated/repeating block of code to reduce bloat would be great -- on top of everything else.
Instead of having the if statement inside the while, you should combine the two conditions to avoid duplicating that code:
while ( (cin >> val) && (val > 0) )
Also, you need to initialize min to a largest value, rather than zero, if you want the first comparison to capture the first possible value for min.
Making a function out of duplicated code is a general purpose solution that isn't a good choice in your case for two reasons: First, it isn't necessary, since it is easier and better to combine the flow of control so there is no need to invoke that code in two places. Second there are too many local variables used in the duplicated code, so if there were a reason to make the duplicated code into a function, good design would also demand collecting some or all of those local variables into an object.
If it had not been cleaner and easier to merge the two conditions, it still would be better to merge the flow of control than to invent the function to call from two places. You cold have used:
if (val <= 0)
{
break;
}

Finding the smallest number and largest number from a list of random numbers generated

I'm new here and this forum has been a great help! Unfortunately, I'm not able to find the answer to my issue here or anywhere else on the web. I was hoping some of you can give me some help or tips on how to go about this.
The program will generate random numbers based on the max limit and the amount of random numbers that will generate.
I'm also required to find the smallest, largest number, as well as the average from all the numbers generated in the loop. The average I can find using the sum/MAX_COUNT_NUM. Unfortunately, I am stuck finding the smallest and largest number. Been at this for the past 6 hours. Please help anyway you can. Thank you.
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int main(){
int UP_MAX, MAX_COUNT_NUM, RAND_NUM, MIN_COUNT_NUM;
cout << "This program creates random numbers" << "\n" << "\n";
cout << "Enter the upper limit of all generated random numbers: ";
cin >> UP_MAX;
cout << "\n" << "\n";
cout << "Enter the count of random numbers: ";
cin >> MAX_COUNT_NUM;
cout << "\n" << "\n";
cout << "Creating " << MAX_COUNT_NUM << " random numbers from 1 to " << UP_MAX << ": " << "\n" << "\n";
MIN_COUNT_NUM = 1;
int LARGE = 0;
int SMALL = 0;
for (; MAX_COUNT_NUM >= MIN_COUNT_NUM; MIN_COUNT_NUM++)
{
RAND_NUM = rand() % UP_MAX + 1;
cout << setw(8) << RAND_NUM;
if (RAND_NUM < SMALL)
{
SMALL = UP_MAX + 1;
}
if (RAND_NUM > LARGE)
{
LARGE = RAND_NUM;
}
}
Unfortunately, I need to do this without arrays and vectors. In my head, I'm thinking it should work as well but it doesn't. The largest number comes out fine, but the smallest comes out as 0 which makes me scratch my head.
I'm taking a beginners course and this got me stumped, so my way of thinking may be off beat. If there are any other tips you can provide, I definitely appreciate it.
The problem is with the initial values that you picked for LARGE and SMALL: you set both of them to 0, which is incorrect: you should set them both to the first random number that you generate.
Alternatively, you can set SMALL to the largest possible int, and LARGE to the smallest possible int. Use <limits> header and std::numeric_limits<int> class.
Note: SMALL = UP_MAX + 1; should be SMALL = RAND_NUM;

not random enough for monte carlo

I am trying to generate values from a normal distribution using a monte carlo method, as per the website http://math60082.blogspot.ca/2013/03/c-coding-random-numbers-and-monte-carlo.html
I modified the code a bit from the original so it calculates the variance and mean for the numbers generated directly to check if the method is working rather than do the tests separately (same difference really but just a heads up).
Question
Regardless of what I do, the variance is way above 1 and the mean is not zero. Is it possible the pseudo-random numbers generated aren't random enough?
Code
PLEASE NOTE THAT THE AUTHOR OF THE ABOVE GIVEN WEBSITE IS THE PERSON WHO WROTE THE CODE
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <iostream>
using namespace std;
// return a uniformly distributed random number
double uniformRandom()
{
return ( (double)(rand()) + 1. )/( (double)(RAND_MAX) + 1. );
}
// return a normally distributed random number
double normalRandom()
{
double u1=uniformRandom();
double u2=uniformRandom();
return cos(8.*atan(1.)*u2)*sqrt(-2.*log(u1));
}
int main()
{
double z;
int N=1000;
double array[N];
double mean=0 ,variance=0;
srand(time(NULL));
for(int i=0;i<N;i++)
{
z=normalRandom();
cout << i << "->"<< z<< endl;
mean+=z;
array[i]=z;
}
mean=mean/N ;
cout << " mean = " << mean << endl;
for(int i=0;i<N;i++)
{
variance = variance + (mean - array[i])*(mean - array[i]);
}
variance = variance/N;
cout << " variance = " << variance << endl;
return 0;
}
UPDATE
Apparently as pointed by users, I screwed up and the program was not working because of a very silly mistake.
You seems computed the mean in a wrong way. mean should be averaged over N, while you only sum over all array elements. current mean is actually sum.
mean = mean /N
rand() is a very low quality random numbers generator in most implementations. Some Linux versions would take value from kernel entropy pool, but it is not guaranteed across platforms (e.g. on Windows?) Use a Mersenne Twister instead. Boost libraries implement one.
EDIT: taocp answer highlights a coding problem, but the RNG issue still applies.