Better random algorithm?

后端 未结 14 1774
情书的邮戳
情书的邮戳 2020-12-16 06:48

I\'m making a game in C++ and it involves filling tiles with random booleans (either yes or no) whether it is yes or no is decided by rand() % 1. It doesn\'t fe

相关标签:
14条回答
  • 2020-12-16 07:16

    Knuth suggests a Random number generation by subtractive method. Its is believed to be quite randome. For a sample implementation in the Scheme language see here

    0 讨论(0)
  • 2020-12-16 07:18

    C++11 has the following way of implementing the Mersenne tittie twister algorothm. From cppreference.com:

    #include <random>
    #include <iostream>
    
    int main()
    {
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_int_distribution<> dis(1, 6);
    
        for (int n=0; n<10; ++n)
            std::cout << dis(gen) << ' ';
        std::cout << '\n';
    }
    

    This produces random numbers suitable for simulations without the disadvantages of many other random number generators. It is not suitable for cryptography; but cryptographic random number generators are more computationally intensive.

    There is also the Well equidistributed long-period linear algorithm; with many example implementations.

    0 讨论(0)
  • 2020-12-16 07:18

    The lowest bits of standard random number generators aren't very random, this is a well known problem.

    I'd look into the boost random number library.

    0 讨论(0)
  • 2020-12-16 07:20

    The low order bits are not very random.
    By using %2 you are only checking the bottom bit of the random number.

    Assuming you are not needing crypto strength randomness.
    Then the following should be OK.

    bool  tile = rand() > (RAND_MAX / 2);
    
    0 讨论(0)
  • 2020-12-16 07:21

    Boost Random Number Library

    0 讨论(0)
  • 2020-12-16 07:22

    People say lower-order bits are not random. So try something from the middle. This will get you the 28th bit:

    (rand() >> 13) % 2
    
    0 讨论(0)
提交回复
热议问题