1
\$\begingroup\$

I'm moving outside my comfort zone and trying to make a random number distribution program while also making sure it is still somewhat uniform. I was trying to generate this in debug mode without optimization on. Also, I am using Visual Studio 2015.

RandomDistribution.h file

#pragma once
#include <vector>
#include <random>
#include <iostream>



static float randy(float low, float high) {
    static  std::random_device rd;
    static  std::mt19937 random(rd());
      std::uniform_real_distribution<float> ran(low, high);
    return ran(random);
}


typedef std::vector<float> Vfloat;
class RandomDistribution
{
public:

    RandomDistribution();
    RandomDistribution(float percent, float contents, int container);
    ~RandomDistribution();
    void setvariables(float percent, float contents, int container);
    Vfloat RunDistribution();
private:
    float divider;
    float _percent;
     int jar_limit;
    float _contents;
    float _maxdistribution;
    Vfloat Jar;
    bool is0;
};

RandomDistribution.cpp

#include "RandomDistribution.h"

RandomDistribution::RandomDistribution() {

}
RandomDistribution::RandomDistribution(float percent, float contents, int containers):_contents(contents),jar_limit(containers)
{
    Jar.resize(containers);
    if (percent < 0)
        _percent = 0;

    else {
        _percent = percent;
    }
    divider = jar_limit * percent;
    is0 = false;
}


RandomDistribution::~RandomDistribution()
{
}
void RandomDistribution::setvariables(float percent, float contents, int container) {
    if (jar_limit != container)
        Jar.resize(container);

    _contents = contents;
    jar_limit = container;
    is0 = false;


    if (percent < 0)
        _percent = 0;

    else {
        _percent = percent;
    }
    divider = jar_limit * percent;
}


Vfloat RandomDistribution::RunDistribution() {

    for (int i = 0; i < jar_limit; i++) {

        if (!is0) {
            if (i + 1 >= jar_limit || _contents < 2) {
                Jar[i] = _contents;
                _contents -= Jar[i];
                is0 = true;
            }

            if (!_percent <= 0) {//making sure it does not get the hole container at once
                _maxdistribution = (_contents / (divider)) * (i + 1);
            }
            else {
                _maxdistribution = _contents;
            }

            Jar[i] = randy(0, _maxdistribution);

            if (Jar[i] < 1) {
                Jar[i] = 0;
                continue;
            }

            _contents -= Jar[i];
        }
        else {
            Jar[0];
        }
        //mixing Jar so it is randomly spaced out instead all at the top
        int swapper = randy(0, i);
        float hold = Jar[i];
        Jar[i] = Jar[swapper];
        Jar[swapper] = hold;

    }

    return Jar;
}

Source code

int main(){
    RandomDistribution distribution[100];
    for (int i = 0; i < 100; i++) {
         distribution[i] = {RandomDistribution(1.0f, 5000.0f, 2000) };
    }


    Vfloat k;
    k.resize(200);

    for (int i = 0; i < 10; i++) {
        auto t3 = chrono::steady_clock::now();

        for (int b = 0; b < 100; b++) {

            k = distribution[b].RunDistribution();
            distribution[b].setvariables(1.0f, 5000.0f, 2000);

        }

        auto t4 = chrono::steady_clock::now();
        auto time_span = chrono::duration_cast<chrono::duration<double>>(t4 - t3);
        cout << time_span.count() << " seconds\n";

    }
}

What prints out is usually between 1 to 2 seconds for each cycle. I want to bring it down to a tenth of a second if possible because this is gonna be only one step of the process to completion and I want to run it a lot more than 100 times. What can I do to speed this up? Any trick or something I'm just missing here?

Here is a sample of the timestamps:

  • 4.71113 seconds
  • 1.35444 seconds
  • 1.45008 seconds
  • 1.74961 seconds
  • 2.59192 seconds
  • 2.76171 seconds
  • 1.90149 seconds
  • 2.2822 seconds
  • 2.36768 seconds
  • 2.61969 seconds
\$\endgroup\$
  • 3
    \$\begingroup\$ First step for better performance: Run in Release mode with optimizations cranked up... Seriously, try it. Using debug mode without optimizations for performance is like trying to outrun a turtle by sitting on it. \$\endgroup\$ – hoffmale Nov 19 '17 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.