[C++]数据生成器、随机序列生成器、排序生成器

例如为了在刷(出)算法题中出随机的测试用例,可用本程序生成数据

包含两个函数,已封装,看看函数的参数是什么然后调用即可

第一个函数是生成[l, r]的一个随机排列

        例[1,10]可能会生成1 3 2 6 8 5 4 9 7 10 

第二个函数是生成范围为[l, r]的长度为n的随机序列

        例[-100,0]可能会生成-27 -90 -22 -58 -95 -75 -57 -57 -1 -42 
理论上可以生成相当大的测试数据,但你可能无法在短时间内跑完,且会占用相当大的储存空间

例如生成一个范围为[1,1e6]的1e9个数的随机序列,程序在几分钟内完成了,生成文件大小为6.41GB

 

可以看到,这个程序可以生成大量测试数据。但是一般来说,我们并不需要生成这么庞大的数据

以下是程序代码 

#include <iostream>
#include <fstream>
#include <random>
#include <string>
using namespace std;
typedef long long __type;   //此处可更改,例如将long long改成更大的__int128 (在支持__int128的前提下)
//请包含以上代码

__type* random_permutation(string path, __type l, __type r, bool return_switch = false, double entropy = 1.0, bool in_switch = true) {
    /// @brief 可生成一个[l,r]内r-l+1个数的随机排列并写入文件,[l,r]中内每个值仅出现一次
    /// @param path 文件路径
    /// @param l 闭区间左端点
    /// @param r 闭区间右端点
    /// @param entropy 熵,数组混乱程度,默认为1.0,即100%,必须为大于等于0的浮点数
    /// @param return_switch 是否返回数组,默认为false,是false则会释放掉数组而返回NULL
    /// @param in_switch 写入开关,为false则不写入文件,默认为true
    /// @return 当return_switch为true时返回所生成序列的数组,为false则返回NULL
    __type len = abs(r - l + 1);
    ofstream __ofs;

    if (in_switch) {
        __ofs.open(path, ios::app);
    }

    if (!__ofs.is_open()) {
        in_switch = false;
        cout << "创建文件对象失败,无法写入" << endl;
    }

    default_random_engine e(time(0));
    srand((unsigned)time(NULL));
    uniform_int_distribution<unsigned> u(0, len - 1);
    
    __type *__arr;
   __arr = (__type *)malloc(len * sizeof(__type));

    if (__arr == NULL) {
        __ofs.close();
        cout << "写入失败" << endl;
        return NULL;
    }
    
    for (__type i = 0, t = l; i < len; i++, t++) {
        __arr[i] = t;
    }
    
    for (__type i = 0; i < len; i++) {
        __type idxa = u(e);
        __type idxb = u(e);
        swap(__arr[idxa], __arr[idxb]);
    }
    
    if (in_switch) {
        for (__type i = 0; i < (__type) entropy * len; i++) {
            __ofs << __arr[i] << ' ';
        }
        __ofs << endl;
        __ofs.close();
        cout << "写入完成" << endl;
    }

    if (return_switch) return __arr;
    free(__arr);
    return NULL;
}

__type* random_permutation(__type n, string path, __type l, __type r, bool return_switch = false, bool in_switch = true) {
    /// @brief 可写入并返回一个范围为[l,r]长度为n的数组到路径path
    /// @param n 数组长度,即数组元素个数
    /// @param path 文件路径
    /// @param l 闭区间左端点
    /// @param r 闭区间右端点
    /// @param return_switch 是否返回数组,默认为false,是false则不会生成数组而返回NULL
    /// @param in_switch 写入开关,为false则不写入文件,默认为true
    /// @return 当return_switch为true时返回所生成序列的数组,为false则返回NULL
    ofstream __ofs;

    if (in_switch) {
        __ofs.open(path, ios::app);
    }
    
    if (!__ofs.is_open() && in_switch) {
        in_switch = false;
        cout << "创建文件对象失败,无法写入" << endl;
    }

    default_random_engine e(time(0));
    srand((unsigned)time(NULL));
    uniform_int_distribution<> u(l,r);

    __type *__arr = NULL;
    if(return_switch) __arr = (__type *)malloc(n * sizeof(__type));

    if (__arr == NULL && return_switch) {
        cout << "返回数组失败" << endl;
        return_switch = false;
    }

    for (__type i = 0, temp = u(e); i < n; i++) {
        temp = u(e);
        if(return_switch) __arr[i] = temp;
        if(in_switch) __ofs << temp << ' ';
    }

    if (in_switch) {
        __ofs << endl;
        __ofs.close();
        cout << "写入完成" << endl;
    }

    if (return_switch) return __arr;
    free(__arr);
    return NULL;
}

int main() {
    // C++支持重载,以下是用例
    string p1 = "test1.txt";
    random_permutation(p1, -10, 10);
    cout << "写入[-10,10]的21个数的一个随机排列完成" << endl;
    string p2 = "test2.txt";
    random_permutation(100, p2, -100, 100);
    cout << "写入范围为[-100,100]的100个元素的随机序列完成" << endl;
    return 0;
}

以下是上述代码的生成文件(供参考)

 

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值