C++生成随机数并存储到txt文件

C++生成随机数并存储到txt文件

  • 引言
  • 1.最简单的生成
    • 1.1 函数介绍
    • 1.2 范围
    • 1.3 产生更大范围的随机数
  • 2.梅森旋转mt19937
    • 2.1 无范围
    • 2.2 有范围
    • 2.3 将容器里的数据打乱
      • 2.3.1 shuffle函数
      • 2.3.2 copy函数
      • 2.3.3 流迭代器
      • 2.3.4 代码示例
    • 2.4 for循环产生大量随机数示例代码
  • 3. 生成随机数并存储到txt文件

引言

生成随机数经常用,还得经常存储到文件当中。
很讨厌莫名其妙的博客扔个代码不详细解释各个函数的用法和意义的,因此还是得自己写个,还有收费打赏机制差不多得了流汗

1.最简单的生成

1.1 函数介绍

rand()返回0~RAND_MAX(32767即2^15 - 1)的整数,不用参数
srand()函数原型:void srand(unsigned int seed);
time()会返回1970年1月1日至今所经历的时间(以秒为单位),需要引入头文件,由于单位原因,注意time()的参数不变时,一秒内time()的取值不会改变

#include 
#include 
using namespace std;

int main()
{
    srand((int)time(0));  // 随机数种子  把0换成NULL也行
    for (int i = 0; i < 10; i++)
    {
        cout << rand() % 100 << " ";//生成0-99随机数
    }
    return 0;
}

1.2 范围

要取得[0,n) 就是rand() % n 表示 从0到n-1的数

要取得[a,b)的随机整数,使用(rand() % (b-a))+ a;
要取得[a,b]的随机整数,使用(rand() % (b-a+1))+ a;
要取得(a,b]的随机整数,使用(rand() % (b-a))+ a + 1;
通用公式:a + rand() % n;其中的a是起始值,n是整数的范围。
要取得a到b之间的随机整数,另一种表示:a + (int)b * rand() / (RAND_MAX + 1);
要取得0~1之间的浮点数,可以使用rand() / double(RAND_MAX)。

#include 
#include 
#define random(a,b) (rand()%(b-a)+a)//使随机数函数更加便于使用

using namespace std;

int main()
{
    srand((int)time(0));  // 产生随机种子  把0换成NULL也行
    for (int i = 0; i < 10; i++)
    {
        cout << random(5, 10) << " ";
    }
    return 0;
}

1.3 产生更大范围的随机数

rand()产生的数是0~RAND_MAX( 0x7fff),也就是说,只能生成15位。如果需要32位的,可以进行扩展:

unsigned int rand_32()
{
	return (rand()&0x3)<<30 | rand()<<15 | rand();
}
#include
#include
#include
#include
using namespace std;
unsigned int rand_32()
{
	return (rand()&0x3)<<30 | rand()<<15 | rand();
}
int main()
{
	srand(time(NULL));
	unsigned a;
	for(int i = 0; i < 10; i++)
	{
		a = rand_32();
		cout << hex << a << endl;
	}
	return 0;
}

参考https://blog.csdn.net/sinat_41909065/article/details/82960361

2.梅森旋转mt19937

想生成又快又好的随机数,还得mt19937

2.1 无范围

#include 
#include 
#include 
using namespace std;
int main()
{
    // 随机数种子
	unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();//1970-01-01 0:00至今的时间,精确到纳秒
    mt19937 rand_num(seed);	 // 大随机数
	cout << rand_num() << endl;
	return 0;
}

2.2 有范围

#include 
#include 
#include 
using namespace std;
int main()
{
	// 随机数种子
	unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
	mt19937 rand_num(seed);
	uniform_int_distribution<long long> dist(0, 1000000000);  //定义随机数生成器 给定范围
	cout << dist(rand_num) << endl;
	return 0;
}

上述代码中的 rand_num 和 dist 都是自己定义的对象名称

2.3 将容器里的数据打乱

2.3.1 shuffle函数

函数原型:
void shuffle (RandomAccessIterator first, RandomAccessIterator last, URNG&& g);
第三个参数是唯一随机数生成器的一个实例,不填的话默认参数是random的默认随机数生成器
函数功能:使用随机生成器g对元素[first, last)可交换的容器内部元素进行随机排列,类似于下面代码所实现功能

template <class RandomAccessIterator, class URNG>
void shuffle (RandomAccessIterator first, RandomAccessIterator last, URNG&& g)
{
  for (auto i = (last-first) - 1; i > 0; --i)
  {
    std::uniform_int_distribution<decltype(i)> d (0,i);
    swap (first[i], first[d (g)]);
  }
}

2.3.2 copy函数

函数原型:std::copy(start, end, container);
start,end是需要复制的容器的头地址和尾地址,container是接收器的起始地址。
copy两个用法:
用法一:将数组myints中的七个元素复制到myvector容器中

 copy ( myints, myints+7, myvector.begin() );  

用法二:将数组myints中的元素向左移动一位

 copy(myints + 1, myints + 7, myints);

2.3.3 流迭代器

ostream_iterator是流迭代器,标准模板库中的。因此是类模板。
ostream_iterator
指定了类型,就是迭代器读写的类型。
通过这个流迭代器可以把你要输入的写入到指定的流中。
cout就是指定的流,这里是标准输出,可以改成一个输出流就可以,比如一个文件。
通俗的一点说,你把它看成一个指向输出流的指针。通过这个指针你可以把东西写到输出流中。

把向量V中的数据放到cout输出流中,通过流迭代器进行output操作。

copy (v.begin(),v.end(),output);

下面意思是放到输出流的时候,每放一个int类型数据,就末尾添加一个*

ostream_iterator<int> output(cout ,"*");

2.3.4 代码示例

#include 
#include 
#include 
#include 
using namespace std;

int main()
{
    vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    random_device rd;
    mt19937 g(rd());

    shuffle(v.begin(), v.end(), g);

    copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
    cout << "\n";

    system("pause");
    return 0;
}

2.4 for循环产生大量随机数示例代码

#include 
#include 
#include 
using namespace std;
const int MAX = 999983;//没什么用,但这个数是质数
int arr[MAX];
int main()
{
    mt19937 rand(time(0));
    for (int i = 0; i < 500000; ++i) //产生大量随机数
    {
        int input = rand() % 998244353;//对一个大的质数取模,一般1e9+7、1e9+9或者998244353,这三个都是质数并且小于2^30
        arr[i] = input;
    }

    return 0;
}

3. 生成随机数并存储到txt文件

#include 
#include 
#include
#define random(a, b) (rand() % (b - a) + a) //使随机数函数更加便于使用

using namespace std;

int main()
{
    ofstream out;
    out.open("data.txt",ios::app);

    srand((int)time(0)); // 产生随机种子  把0换成NULL也行
    for (int i = 0; i < 100; i++)
    {
        out << random(1, 10000) << " ";
    }
    out<<"\n";
    out.close();
    return 0;
}

你可能感兴趣的:(C++常用,c++)