Modified random distribution: Difference between revisions

New post.
m (Improved coding.)
(New post.)
Line 215:
0.92..0.96 *******************************************************************
0.96..1.00 ************************************************************************</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cmath>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <random>
#include <string>
#include <vector>
 
std::random_device random;
std::mt19937 generator(random());
std::uniform_real_distribution<double> distribution(0.0F, 1.0F);
 
double modifier(const double& x) {
return ( x < 0.5 ) ? 2 * ( 0.5 - x ) : 2 * ( x - 0.5 );
}
 
double modified_random(const std::function<double(double)>& modify) {
double result = -1.0;
 
while ( result < 0.0 ) {
double random_one = distribution(generator);
double random_two = distribution(generator);
if ( random_two < modify(random_one) ) {
result = random_one;
}
}
return result;
}
 
int main() {
const int32_t sample_size = 100'000;
const int32_t bin_count = 20;
const double bin_size = 1.0 / bin_count;
 
std::vector<int32_t> bins(bin_count, 0);
 
for ( int32_t i = 0; i < sample_size; ++i ) {
double random = modified_random(modifier);
int32_t bin_number = floor(random / bin_size);
bins[bin_number]++;
}
 
std::cout << "Modified random distribution with " << sample_size << " samples in range [0, 1):" << "\n" << std::endl;
std::cout << " Range Number of samples within range" << std::endl;
 
const int32_t scale_factor = 125;
for ( float i = 0.0; i < bin_count; ++i ) {
std::string histogram = " " + std::string(bins[i] / scale_factor, '#') + " ";
std::cout << std::fixed << std::setw(4)<< std::setprecision(2) << i / bin_count << " ..< "
<< std::setw(4) << ( i + 1.0 ) / bin_count << histogram << bins[i] << std::endl;
}
}
</syntaxhighlight>
{{ out }}
<pre>
Modified random distribution with 100000 samples in range [0, 1):
 
Range Number of samples within range
0.00 ..< 0.05 ############################################################################ 9581
0.05 ..< 0.10 ##################################################################### 8662
0.10 ..< 0.15 ############################################################ 7516
0.15 ..< 0.20 ################################################### 6405
0.20 ..< 0.25 ############################################ 5595
0.25 ..< 0.30 #################################### 4502
0.30 ..< 0.35 ########################### 3464
0.35 ..< 0.40 ################### 2466
0.40 ..< 0.45 ############ 1525
0.45 ..< 0.50 #### 508
0.50 ..< 0.55 ### 482
0.55 ..< 0.60 ########### 1479
0.60 ..< 0.65 ################### 2494
0.65 ..< 0.70 ########################### 3440
0.70 ..< 0.75 ##################################### 4656
0.75 ..< 0.80 ############################################ 5544
0.80 ..< 0.85 ################################################### 6483
0.85 ..< 0.90 ########################################################### 7411
0.90 ..< 0.95 ################################################################### 8389
0.95 ..< 1.00 ########################################################################### 9398
</pre>
 
=={{header|Factor}}==
871

edits