13.2 Test Lot

(Win version)

< 13.1 | 13.2 | 14.0 >

Take the Lot class for a little spin.

The following main function gives the functions in the Lot class a test. Just replace all of main.cpp with the following:

#include &lt;iostream&gt; 
#include "lot.hpp"

using namespace strom;

int main(int argc, const char * argv[]) {
    Lot lot;
    lot.setSeed(12345);
    std::cout &lt;&lt; "Seed set to 12345" &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Uniform(0,1) random deviate:             " &lt;&lt; lot.uniform() &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Uniform(0,1) random deviate (log scale): " &lt;&lt; lot.logUniform() &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Discrete Uniform(1,4) random deviate:    " &lt;&lt; lot.randint(1,4) &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Normal(0,1) random deviate:              " &lt;&lt; lot.normal() &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Gamma(2,1) random deviate:               " &lt;&lt; lot.gamma(2.0,1.0) &lt;&lt; std::endl;
    lot.setSeed(12345);
    std::cout &lt;&lt; "\nSeed set to 12345" &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Uniform(0,1) random deviate:             " &lt;&lt; lot.uniform() &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Uniform(0,1) random deviate (log scale): " &lt;&lt; lot.logUniform() &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Discrete Uniform(1,4) random deviate:    " &lt;&lt; lot.randint(1,4) &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Normal(0,1) random deviate:              " &lt;&lt; lot.normal() &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Gamma(2,1) random deviate:               " &lt;&lt; lot.gamma(2.0,1.0) &lt;&lt; std::endl;
    
    return 0;   
}   

Running the program now should produce the output below:

Seed set to 12345
  Uniform(0,1) random deviate:             0.929616
  Uniform(0,1) random deviate (log scale): -0.11636
  Discrete Uniform(1,4) random deviate:    2
  Normal(0,1) random deviate:              0.722622
  Gamma(2,1) random deviate:               1.21748

Seed set to 12345
  Uniform(0,1) random deviate:             0.929616
  Uniform(0,1) random deviate (log scale): -0.11636
  Discrete Uniform(1,4) random deviate:    2
  Normal(0,1) random deviate:              0.722622
  Gamma(2,1) random deviate:               1.21748

This simply demonstrates that, after resetting the seed to the same value, the subsequent random deviates generated are identical to those generated the first time around (using that same seed).

Feel free to add more tests of the pseudorandom numbers generated by Lot. For example, the mean of several thousand deviates generated using lot.gamma(2.0,3.0) should be close to 6.0 (the mean of a gamma distribution is the shape multiplied by the scale). Adding the part shown in blue below would test this:

#include &lt;iostream&gt; 
#include "lot.hpp"

using namespace strom;

int main(int argc, const char * argv[]) {
    Lot lot;
    lot.setSeed(12345);
    std::cout &lt;&lt; "Seed set to 12345" &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Uniform(0,1) random deviate:             " &lt;&lt; lot.uniform() &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Uniform(0,1) random deviate (log scale): " &lt;&lt; lot.logUniform() &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Discrete Uniform(1,4) random deviate:    " &lt;&lt; lot.randint(1,4) &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Normal(0,1) random deviate:              " &lt;&lt; lot.normal() &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Gamma(2,1) random deviate:               " &lt;&lt; lot.gamma(2.0,1.0) &lt;&lt; std::endl;
    lot.setSeed(12345);
    std::cout &lt;&lt; "\nSeed set to 12345" &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Uniform(0,1) random deviate:             " &lt;&lt; lot.uniform() &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Uniform(0,1) random deviate (log scale): " &lt;&lt; lot.logUniform() &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Discrete Uniform(1,4) random deviate:    " &lt;&lt; lot.randint(1,4) &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Normal(0,1) random deviate:              " &lt;&lt; lot.normal() &lt;&lt; std::endl;
    std::cout &lt;&lt; "  Gamma(2,1) random deviate:               " &lt;&lt; lot.gamma(2.0,1.0) &lt;&lt; std::endl;
    
    <span style="color:#0000ff"><strong>std::cout &lt;&lt; "\nMean of 10000 Gamma(2,3) deviates is expected to be 6.0:" &lt;&lt; std::endl;</strong></span>
    <span style="color:#0000ff"><strong>double total = 0.0;</strong></span>
    <span style="color:#0000ff"><strong>for (unsigned i = 0; i &lt; 10000; ++i)</strong></span>
        <span style="color:#0000ff"><strong>total += lot.gamma(2.0,3.0);</strong></span>
    <span style="color:#0000ff"><strong>std::cout &lt;&lt; "  sample mean = " &lt;&lt; (total/10000.0) &lt;&lt; std::endl;</strong></span>

    return 0;   
}   

< 13.1 | 13.2 | 14.0 >