Generate a random number with normal distribution (googleUnitTest)

Generate a random number with normal distribution with mean 0 and deviation 1

#include <iostream>
#include <random>
using namespace std;
int main()
{
    // generate a random number with normal distribution with mean 0 and standard deviation 1
    std::default_random_engine generator;
    std::normal_distribution<double> distribution(0.0, 1.0);
    double number = distribution(generator);
    cout << number << endl;
    return 0;
}

The problem for above codes:

  1. Run it in different times and get the same results:

    ubuntu@VM-0-5-ubuntu:~$ ./a.out 
    -0.121966
    ubuntu@VM-0-5-ubuntu:~$ ./a.out 
    -0.121966
    
  2. The result is double data type which is not what I want.

The First Update Version Starting Random

  1. The Source Codes:

    #include <iostream>
    #include <random>
    using namespace std;
    int main()
    {
        // generate a random number with normal distribution with mean 0 and standard deviation 1
        std::random_device rd;
        std::mt19937 gen(rd());
        std::normal_distribution<> dis(0, 1);
        for (int n = 0; n < 10; ++n) {
            std::cout << "The " << n << "th random number is: " << dis(gen) << '\n';
        }
        std::cout << '\n';
        return 0;
    }
    

The results:

ubuntu@VM-0-5-ubuntu:~$ ./a.out 
The 0th random number is: -0.125937
The 1th random number is: -0.0305842
The 2th random number is: 0.0240349
The 3th random number is: 1.18841
The 4th random number is: -0.209324
The 5th random number is: -0.293314
The 6th random number is: -0.0231207
The 7th random number is: 2.22857
The 8th random number is: -0.407497
The 9th random number is: 0.184923

The second update version

#include <iostream>
#include <random>

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::normal_distribution<> d(50, 15);

    int result = static_cast<int>(d(gen));
    result = std::max(0, result);
    result = std::min(100, result);
    std::cout << result << std::endl;

    return 0;
}

The Results for above codes

ubuntu@VM-0-5-ubuntu:~$ ./a.out 
40
ubuntu@VM-0-5-ubuntu:~$ ./a.out 
7
ubuntu@VM-0-5-ubuntu:~$ ./a.out 
56
ubuntu@VM-0-5-ubuntu:~$ ./a.out 
29
ubuntu@VM-0-5-ubuntu:~$ ./a.out 
54
ubuntu@VM-0-5-ubuntu:~$ ./a.out 
40

Test above codes whether meets our expections

There are several ways to test the correctness of code. One common approach is to use unit testing. Unit tests are small, automated tests that check individual units of code (such as functions or methods) to ensure that they behave as expected. You can write unit tests using a testing framework such as Google Test for C++.

Another approach is to use integration testing, which tests how different units of code work together. Integration tests can help catch issues that may not be apparent when testing individual units in isolation.

In addition to automated testing, it’s also important to perform manual testing to verify that the code behaves correctly when run by a user. This can involve running the code and interacting with it to ensure that it produces the expected results.

It’s also a good practice to use code reviews to have other developers review your code and provide feedback on its correctness and quality.

Overall, testing is an important part of the software development process that can help ensure the correctness and reliability of your code. It’s a good idea to use a combination of different testing techniques to thoroughly test your code.

The Unit Test for Above codes

Here’s an example of how you can write a unit test for the code I provided earlier using the Google Test framework for C++:

#include <gtest/gtest.h>
#include <random>

int generateRandomNumber() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::normal_distribution<> d(50, 15);

    int result = static_cast<int>(d(gen));
    result = std::max(0, result);
    result = std::min(100, result);

    return result;
}

TEST(RandomNumberTest, GeneratesNumberBetweenZeroAndHundred) {
    for (int i = 0; i < 1000; i++) {
        int result = generateRandomNumber();
        EXPECT_GE(result, 0);
        EXPECT_LE(result, 100);
    }
}

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

This test uses the TEST macro from Google Test to define a test case named RandomNumberTest with a test named GeneratesNumberBetweenZeroAndHundred. The test calls the generateRandomNumber function (which contains the code from my previous response) 1000 times and uses the EXPECT_GE and EXPECT_LE macros to check that the generated number is between 0 and 100.

You can compile and run this test using a command like this:

g++ -std=c++11 -isystem /usr/local/include -pthread random_number_test.cpp -L/usr/local/lib -lgtest -lgtest_main -o random_number_test
./random_number_test

This assumes that you have installed Google Test on your system and that its headers and libraries are located in /usr/local/include and /usr/local/lib, respectively. You may need to adjust these paths if your installation is different.

I hope this helps! Let me know if you have any questions.

Install googleTest to your computer

Google Test is a C++ testing framework that can be installed on various operating systems. Here are the general steps to install Google Test on your system:

  1. Download the source code: You can download the latest release of Google Test from its GitHub repository. Alternatively, you can clone the repository using git:

    git clone https://github.com/google/googletest.git
    
  2. Build the library: Google Test uses CMake as its build system. To build the library, navigate to the directory where you downloaded or cloned the source code and create a new directory for the build:

    cd googletest
    mkdir build
    cd build
    

    Then, run cmake to generate the build files:

    cmake ..
    

    Finally, run make to build the library:

    make
    
  3. Install the library: Once the library is built, you can install it on your system by running make install with administrative privileges:

    sudo make install
    

    This will install the Google Test headers and libraries in the default system locations (usually /usr/local/include and /usr/local/lib, respectively).

After completing these steps, you should be able to use Google Test in your C++ projects by including its headers and linking against its libraries.

Please note that these instructions are general and may vary depending on your specific operating system and environment. You can find more detailed instructions for installing Google Test in its documentation.

I hope this helps! Let me know if you have any further questions.

Run the test codes

  1. Check the command:
g++ -isystem /usr/local/include/ -pthread unit_test.cpp -lgtest -lgtest_main -o unit_test
  1. The Results
ubuntu@VM-0-5-ubuntu:~$ ./unit_test 
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from RandomNumberTest
[ RUN      ] RandomNumberTest.GeneratesNumberBetweenZeroAndHundred
[       OK ] RandomNumberTest.GeneratesNumberBetweenZeroAndHundred (25 ms)
[----------] 1 test from RandomNumberTest (25 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (25 ms total)
[  PASSED  ] 1 test.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值