For a project that I am working on I need to generate a vector of random numbers within a function. The problem is that I end up generating the same vector of numbers each time. I have this example that reproduces my problem:
#include <iostream>
#include <random>
std::vector<double> generate(std::default_random_engine generator, double mean, double sigma, int n)
{
std::vector<double> generated(n,0);
std::normal_distribution<double> distribution(mean,sigma);
for (int i = 0;i<n;i++)
generated[i] = distribution(generator);
return generated;
}
std::vector<double> generate(double mean, double sigma, int n)
{
std::vector<double> generated(n,0);
std::default_random_engine generator;
std::normal_distribution<double> distribution(mean,sigma);
for (int i = 0;i<n;i++)
generated[i] = distribution(generator);
return generated;
}
int main(int argc, char** argv)
{
// Read inputs
int nrolls = 20; // number of experiments
int ntimes = 50;
double mean = 100;
double sigma = 4;
bool useFunction(false);
if (argc>1)
useFunction=true;
// crates series
std::vector< std::vector<double> > results(ntimes,std::vector<double>());
std::default_random_engine generator;
for (int i = 0;i<ntimes/4;i++){
std::vector<double> generated(nrolls,0);
std::normal_distribution<double> distribution(mean,sigma);
for (int i = 0;i<nrolls;i++)
generated[i] = distribution(generator);
results[i] = generated;
}
for (int i = ntimes/4;i<ntimes/2;i++)
results[i] = generate(generator,mean,sigma,nrolls);
for (int i = ntimes/2;i<3*ntimes/4;i++){
std::vector<double> generated(nrolls,0);
std::normal_distribution<double> distribution(mean,sigma);
for (int i = 0;i<nrolls;i++)
generated[i] = distribution(generator);
results[i] = generated;
}
for (int i = 3*ntimes/4;i<ntimes;i++)
results[i] = generate(mean,sigma,nrolls);
//
// Display all random numbers
for (int i = 0;i<ntimes;i++){
std::cout<<i;
for (int j = 0;j<nrolls;j++)
std::cout<<" "<<results[i][j];
std::cout<<std::endl;
}
// Check number of equal results
int n_equal(0);
int n_total(0);
for (int i=0;i<ntimes;i++){
for (int k = 0;k<nrolls;k++){
for (int j=i+1;j<ntimes;j++){
n_total++;
if (results[i][k] == results[j][k])
n_equal++;
}
}
}
std::cout<<n_equal<<"/"<<n_total<<std::endl;
// Exit
return 0;
}
I have tried to solve it by passing the generator to the function where the array of random numbers is generated but apparently, it does not work either. Can somebody give me a hint on how should I do it to get different arrays each time i call the generate function?
Thank you very much.
You have two problems here. First
std::vector<double> generate(std::default_random_engine generator, double mean, double sigma, int n)
Takes the PRNG by value, which means it makes a copy. That means every time you call the function your going to be starting from the same sequence since you never modify the generator from the call site.
The second issue is with
std::vector<double> generate(double mean, double sigma, int n)
You recreate the same generator every time you call the function. This is not going to work as it is going to create the same sequence each time.
Typically you have two options. You can pass the PRNG to the function by reference, or you declare a static PRNG in the function so it persists between function calls.
After playing a bit with them, I found it best to use global variables for the new C++ random generators. And you should have one per random number suite, so you're (statistically almost :) 100% sure to get the distribution specified.
Pseudo-random generators are static beasts by nature, since they keep numbers generated in the last computation to generate the next.
Related
I want to count GCD of integers and save them. I find that the time consuming part is not to calculate GCD but to save result to the map. Do I use std::map in a bad way?
#include <set>
#include <iostream>
#include <chrono>
#include "timer.h"
using namespace std;
int gcd (int a, int b)
{
int temp;
while (b != 0)
{
temp = a % b;
a = b;
b = temp;
}
return(a);
}
int main() {
map<int,int> res;
{
Timer timer;
for(int i = 1; i < 10000; i++)
{
for(int j = 2; j < 10000; j++)
res[gcd(i,j)]++;
}
}
{
Timer timer;
for(int i = 1; i < 10000; i++)
{
for(int j = 2; j < 10000; j++)
gcd(i, j);
}
}
}
6627099us(6627.1ms)
0us(0ms)
You should use some real benchmarking library to test this kind of code. In your particular case, the second loop where you discard the results of gcd was probably optimized away. With quickbench I see not that much difference between running just the algorithm and storing the results in std::map or std::unordered_map. I used randomized integers for testing, which is maybe not the best for GCD algorithm, but you can try other approaches.
Code under benchmark without storage:
constexpr int N = 10000;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distrib(1, N);
benchmark::DoNotOptimize(gcd(distrib(gen), distrib(gen)));
and with storage:
benchmark::DoNotOptimize(res[gcd(distrib(gen), distrib(gen))]++);
Results:
You are using std::map correctly. However, you are using an inefficient container for your problem. Given that the possible values of gcd(x,y) are bounded by N, a std::vector would be the most efficient container to store the results.
Specifically,
int main() {
const int N = 10'000;
std::vector<int> res(N, 0); // initialize to N elements with value 0.
...
}
Using parallelism will speed up the program even further. Each thread would have it's own std::vector to compute local results. Once a thread is finished, the results would be added to the result vector in a thread-safe manner (e.g. using std::mutex).
int function(int A[], int n)
{
int i = 0;
int sum = 0;
int amount = 0;
while(i<n) {
if(A[i] > 0) {
sum=sum+A[i];
amount++;
}
else {
i++;
}
}
while(!(i<n)) {
if(ile>0){
return sum/amount;
} else {
return 0;
}
}
}
I am generating random array of numbers between 0-10 , Im trying to use this with this algorithm, but all the time im getting result 6422260. Can someone tell me how should I approach this?
int n;
cin >> n;
int arr[n];
srand(time(NULL));
for (int i = 0; i < 10; i++)
{
arr[i] = rand() % 10;
}
function(arr, n);
Here is a solution to your problem:
#include <random>
#include <iostream>
void fill(int arr[]);
int random(int from, int to);
using namespace std;
int main(void)
{
int arr[10];
fill(arr);
for(int i = 0; i<10; i++)
printf("%d ", arr[i]);
return 0;
}
void fill(int arr[]){
for(int i=0;i<(*(&arr + 1) - arr);i++){
arr[i] = random(0, 10);//adjust accordngly
}
}
int random(int from, int to){
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist6(from, to); // distribution in range [from, to]
return dist6(rng);
}
Your problem is you are not generating random numbers your algorithm is generating the same set of numbers! You need a logic to generate random number. Usually they are generated from system time ...
Attribution : https://stackoverflow.com/a/13445752/14911094
#alkantra, your problem is not generating random numbers. Basically, you are asking your question wrong. If required, it should be separated:
What's this code doing?
How to generate a random sequence?
The algorithm you are trying to achieve is for calculating arithmetic mean (or simply average). If you remember the formula for calculating arithmetic mean you learnt in school, the formula is:
arithmetic mean = sum/n
where
sum - sum of all numbers (from the given array[] of course)
n - count of the numbers in the given array[]
The purpose of the sum variable is to sum all given numbers, if not equal to 0, and n(in your code amount) just increases for every number added to sum.
And in the end the function should return, as the formula says, sum/amount. I could write this code, i.e. the whole program (except for the random()), though it's quite easy, so I'll leave it up to you.
About the random library, I don't know much, but there are may resources on the net, so take your time.
https://www.tutorialspoint.com/cplusplus-program-to-generate-random-number
https://www.tutorialspoint.com/rand-and-srand-in-c-cplusplus
I'm running the following program:
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <chrono>
using namespace std;
const int N = 200; // Number of tests.
const int M = 2000000; // Number of pseudo-random values generated per test.
const int VALS = 2; // Number of possible values (values from 0 to VALS-1).
const int ESP = M / VALS; // Expected number of appearances of each value per test.
int main() {
for (int i = 0; i < N; ++i) {
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
srand(seed);
vector<int> hist(VALS, 0);
for (int j = 0; j < M; ++j) ++hist[rand() % VALS];
int Y = 0;
for (int j = 0; j < VALS; ++j) Y += abs(hist[j] - ESP);
cout << Y << endl;
}
}
This program performs N tests. In each test we generate M numbers between 0 and VALS-1 while we keep counting their appearances in a histogram. Finally, we accumulate in Y the errors, which correspond to the difference between each value of the histogram and the expected value. Since the numbers are generated randomly, each of them would ideally appear M/VALS times per test.
After running my program I analysed the resulting data (i.e., the 200 values of Y) and I realised that some things where happening which I can not explain. I saw that, if the program is compiled with vc++ and given some N and VALS (N = 200 and VALS = 2 in this case), we get different data patterns for different values of M. For some tests the resulting data follows a normal distribution, and for some tests it doesn't. Moreover, this type of results seem to altern as M (the number of pseudo-random values generated in each test) increases:
M = 10K, data is not normal:
M = 100K, data is normal:
and so on:
As you can see, depending on the value of M the resulting data follows a normal distribution or otherwise follows a non-normal distribution (bimodal, dog food or kind of uniform) in which more extreme values of Y have greater presence.
This diversity of results doesn't occur if we compile the program with other C++ compilers (gcc and clang). In this case, it looks like we always obtain a half-normal distribution of Y values:
What are your thoughts on this? What is the explanation?
I carried out the tests through this online compiler: http://rextester.com/l/cpp_online_compiler_visual
The program will generate poorly distributed random numbers (not uniform, independent).
The function rand is a notoriously poor one.
The use of the remainder operator % to bring the numbers into range effectively discards all but the low-order bits.
The RNG is re-seeded every time through the loop.
[edit] I just noticed const int ESP = M / VALS;. You want a floating point number instead.
Try the code below and report back. Using the new <random> is a little tedious. Many people write some small library code to simplify its use.
#include <iostream>
#include <vector>
#include <cmath>
#include <random>
#include <chrono>
using namespace std;
const int N = 200; // Number of tests.
const int M = 2000000; // Number of pseudo-random values generated per test.
const int VALS = 2; // Number of possible values (values from 0 to VALS-1).
const double ESP = (1.0*M)/VALS; // Expected number of appearances of each value per test.
static std::default_random_engine engine;
static void seed() {
std::random_device rd;
engine.seed(rd());
}
static int rand_int(int lo, int hi) {
std::uniform_int_distribution<int> dist (lo, hi - 1);
return dist(engine);
}
int main() {
seed();
for (int i = 0; i < N; ++i) {
vector<int> hist(VALS, 0);
for (int j = 0; j < M; ++j) ++hist[rand_int(0, VALS)];
int Y = 0;
for (int j = 0; j < VALS; ++j) Y += abs(hist[j] - ESP);
cout << Y << endl;
}
}
I'm creating a wxWidget application with C++ where at the start of the program I want the application window to contain pixels with random colors like this:
In the above application there are 3600 pixels (60 x 60) and I have given each pixel a random RGB color by using uniform_int_distribution
The colours for the pixels in the image above are generated at the moment using the following function in my code:
void random_colors(int ctable[][3], int n)
{
// construct a trivial random generator engine from a time-based seed:
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator (seed);
std::uniform_int_distribution<int> distribution(0,255);
for(int i=0; i<n; i++)
{
for(int j=0; j<3; j++)
{
ctable[i][j] = distribution(generator);
}
}
}
I do this by giving this function a table with dimensions 3600 x 3 and this function will fill in the values for the colours.
This way however is not what I want. What I want is to create a class called somNode where each somNode-object represents a pixel in the picture (with RGB values as a member array attribute). In this somNode-class I have a member function using uniform_int_distribution to give when constructed each somNode its own random RGB colour. This is the function which creates the random colour for each somNode:
void rand_node_colour(int nodeWeights[])
{
// construct a trivial random generator engine from a time-based seed:
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator (seed);
std::uniform_int_distribution<int> distribution(0,255);
for(int i=0; i<3; i++)
{
nodeWeights[i] = distribution(generator);
}
}
the nodeWeights member array represents the RGB-value of the somNode. Now when I create this "som-grid" what I have in the image above (the 3600 pixels corresponding to 3600 somNodes) I use the following code (take a look at the som-constructor):
#include "somNode.h"
#include <vector>
class som
{
public:
double learning_rate;
std::vector<somNode> somGrid;
public:
som(double lrate);
void epoch();
void trainOnce();
};
/*
* Initialize the som grid
*/
som::som(double lrate)
{
learning_rate = lrate;
// Create the som grid
for(int i=0; i<60; i++)
{
for(int j=0; j<60; j++)
{
int xL = j*10;
int xR = (j+1)*10;
int yT = i*10;
int yB = (i+1)*10;
somGrid.push_back(somNode(xL, xR, yB, yT));
}
}
}
// Train som by one epoch
void som::epoch()
{
}
// Train som by one color
void som::trainOnce()
{
}
So I have a vector<somNode> somGrid where I push all these 3600 somNodes when I construct them. When each node is constructed the somNode member function rand_node_colour is called which creates the RGB-value.
When however I implement this code instead of the one I used at first I get this result:
You can see that there is a clear pattern so something is going wrong here. My question is: What is happening in the random number generation when somNodes are created? Why it doesn't produce the same result as the code I used above?
P.S. here is the somNode.cpp:
#include <random>
#include <iostream>
#include <chrono>
#include<cmath>
void rand_node_colour(int nodeWeights[]);
/*
* This class represent a node in the som-grid
*/
class somNode
{
public:
// Weight of the node representing the color
int nodeWeights[3];
// Position in the grid
double X, Y;
// corner coorinates for drawing the node on the grid
int x_Left, x_Right, y_Bottom, y_Top;
public:
// Constructor
somNode(int xL, int xR, int yB, int yT);
void editWeights(int r, int g, int b);
double getDistance(int r, int g, int b);
};
somNode::somNode(int xL, int xR, int yB, int yT)
{
// Set the corner points
x_Left = xL;
x_Right = xR;
y_Bottom = yB;
y_Top = yT;
// Initialize random weights for node
rand_node_colour(nodeWeights);
// Calculate the node's position (center coordinate)
X = x_Left + (double)((x_Right - x_Left)/double(2));
Y = y_Bottom + (double)((y_Top - y_Bottom)/double(2));
}
void somNode::editWeights(int r, int g, int b)
{
nodeWeights[0] = r;
nodeWeights[1] = g;
nodeWeights[2] = b;
}
double somNode::getDistance(int r, int g, int b)
{
return sqrt(pow(nodeWeights[0]-r, 2) + pow(nodeWeights[1]-g, 2) + pow(nodeWeights[2]-b, 2));
}
void rand_node_colour(int nodeWeights[])
{
// construct a trivial random generator engine from a time-based seed:
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator (seed);
std::uniform_int_distribution<int> distribution(0,255);
for(int i=0; i<3; i++)
{
nodeWeights[i] = distribution(generator);
}
}
The issue here is that you constantly recreate and seed the random number generator in rand_node_colour. You call it in a tight loop so you can get the same time which means the seed will be the same and that means the random numbers generated will be the same.
What you need to do is seed the generator once and then keep using its random output. An easy way to fix you code would be to make it static in the function sos it is only initialized once and each subsequent call to the function will continue on instead of start the generator all over. If we do that the code becomes
void rand_node_colour(int nodeWeights[])
{
// construct a trivial random generator engine from a time-based seed:
static std::default_random_engine generator (std::chrono::system_clock::now().time_since_epoch().count());
std::uniform_int_distribution<int> distribution(0,255);
for(int i=0; i<3; i++)
{
nodeWeights[i] = distribution(generator);
}
}
I have a problem in generating binary random numbers in C++. I want to make an array and each element contain random numbers in binary form. And I want to XOR elements together. This is my code
void Msg::setXOR(double Code)
{
int array[30];
srand ( time(0) );
for(int j = 0;j<30;j++)
{
i = rand();
array[j]=i
double x = array[j]^ array[j+1]^ array[J+2];
code = x ;
this -> code_var = code
}
}
It doesn't work and it doesn't make binary random numbers. Can anyone help me how should I fix it?
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution;
auto generatef = std::bind(distribution,generator);
std::array<int,30> array;
std::generate(array.begin(), array.end(), generatef);
double x = std::accumulate(array.begin(), array.end(), 0, std::bit_xor<>);
you did not store the generated random num in the array.
int array[30];
srand ( time(0) );
int x=0;
for (int j = 0;j<30;j++)
{
array[j]=rand();
x ^= array[j];
}
return x;