How to make scientific data

If you want a set of informatics race simulation questions, or if you want to shoot their own procedures to verify the correctness of the data generator is essential.

random number

Random data is important. Only enough random data can uniformly cover a variety of situations.

But <cstdlib>Lane rand()is not very good:

  1. It behave differently on different platforms
  2. Its linear recursive algorithm is weak randomness.

At c++11or above the c ++ version, you can use a better mt19937randomization tool.

To: #include<random>

It is based on Mersenne primes, cycle is very long, randomness is also guaranteed. Just a little slower.

We can define based on a mt19937random function

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng2(chrono::steady_clock::now().time_since_epoch().count());

rngThe role and rand()the same, but it needs attention:

  1. rng()\(\in [0, 2^{32})\)
  2. rng2()\(\in [0, 2^{64})\)

chrono::steady_clock::now().time_since_epoch().count()It indicates the current time. It is a random seed.

Defined range of random numbers

//这样来生成一个 1 ~ n 的随机数
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long 
ll rd(ll x) {
    return rng() % x + 1;
}

How to make data and graph theory related?

Generating a random tree method

    for(int i = 2; i <= n; i++) {
        int u = rd(i-1);
        printf("%d %d\n", u, i); //(u, i) 为一条边的两个端点
    }

Generating a random method of FIG Unicom

set<pair<int, int> >  st;
    
    for(int i = 2; i <= n; i++) {
        int u = rd(i-1);
        st.insert(make_pair(u, i));
    }//先生成它的生成树
    while(st.size() < m) {
        int u = rd(n), v = rd(n);
        if(u > v) swap(u, v);
        pair<int, int> pii = make_pair(u, v);
        if(u == v || st.find(pii) != st.end()) continue;
        st.insert(pii);
    }
    //这张图无重边和自环

Guess you like

Origin www.cnblogs.com/Eroad/p/11844279.html