今天小编给大家分享一下C++怎么生成随机浮点数的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
场景描述:
想生成一组整形随机数,放入数组中,用来测试自己的排序是否正确。
于是我写出了下方代码,生成随机数。
先简单了解下用到的函数:
//返回time_t类型的 当前时间的时间戳
time_t time (time_t* timer);
//传入一个种子,为伪随机数生成器初始化
void srand (unsigned int seed);
//得到一个整形伪随机数
int rand (void);
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int arr[10] = { 0 };
for (int i = 0; i < 10; ++i)
{
srand((unsigned int)time(NULL));
//两个相减是为了出现负的随机数,使测试范围更广
arr[i] = (rand() % 100 + 1) - (rand() % 100 + 1);
printf("%d ", arr[i]);
}
return 0;
}
我发现尽管我调用了srand函数,可生成的数组值还是同一个。
我思考后想到,因为for循环执行速度太快,整个程序都是在一秒内完成的。
所以出现了都是同一个值的情况。
于是我想出了下面的解决方法:
我可以在for循环内调用Sleep函数,让我的电脑休眠一下,这样就不会出现上述情况了。
于是我写出了下方的代码:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
int arr[10] = { 0 };
for (int i = 0; i < 10; ++i)
{
Sleep(1000);
srand((unsigned int)time(NULL));
arr[i] = (rand() % 100 + 1) - (rand() % 100 + 1);
printf("%d ", arr[i]);
}
return 0;
}
通过休眠后,就成功解决问题了。
可是,
如果睡眠时间太短,那么还是会出现重复的现象;
如果睡眠时间太长,程序运行速度就太慢。
因为上述的原因,我继续查询资料,了解了rand和srand的基本原理,最终成功解决了该问题。
给srand函数传入一个数值后,srand会根据这个生成一个随机序列表(通常有4,294,967,296个数),传入相同的数生成的序列表是相同的。然后rand从序列的头部取出一个数返回,然后将这个数放在随机序列表尾部,因此如果你要取的数据量非常大,是会出现与之前取出的数重复的情况。
此时,上面出现的问题也很好解决了。因为计算机运行速度很快,所以我们每次进入循环都会生成一个相同的随机序列表,rand函数只会取出其第一个数。
要解决这个问题,我们只需要在循环前调用一次srand函数就好了,这样就不会重复生成序列表了。
下方是最终形式的代码:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int arr[10] = { 0 };
srand((unsigned int)time(NULL));
for (int i = 0; i < 10; ++i)
{
arr[i] = (rand() % 100 + 1) - (rand() % 100 + 1);
printf("%d ", arr[i]);
}
return 0;
}
下文将使用C++11定义在头文件random中的随机数库通过一组协作的类来解决这些问题:随机数引擎类和随机数分布类。
一个引擎类可以生成unsigned随机数序列
一个分布类使用一个引擎类生成指定类型的、在给定范围内的、服从特定概率分布的随机数
uniform_int_distribution:产生均匀分布的整数
template <class IntType = int>
class uniform_int_distribution;
// IntType
// An integer type. Aliased as member type result_type.
// By default, this is int.
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
int main()
{
//产生[1, 100]左闭右闭区间的随机整数
uniform_int_distribution<int> u(1, 100);
default_random_engine e;
//为随机数引擎设置随机种子,若不设置每次生成的随机数相同(可以创建时设置)
//类似srand的用法,相同的种子生成的随机数相同
//default_random_engine e(time(NULL));
e.seed(time(NULL));
for (size_t i = 0; i < 10; ++i)
{
cout << u(e) << " ";
}
cout << endl;
return 0;
}
uniform_real_distribution:产生均匀分布的实数
template <class RealType = double>
class uniform_real_distribution;
// RealType
// A floating-point type. Aliased as member type result_type.
// By default, this is double.
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
int main()
{
//生成[-1, 1]范围随机浮点数
//模板参数只能是浮点类型(float,double, long double)
uniform_real_distribution<double> u(-1, 1);
default_random_engine e(time(NULL));
for (size_t i = 0; i < 10; ++i)
{
cout << u(e) << " ";
}
cout << endl;
return 0;
}
template <class RealType = double>
class normal_distribution;
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
int main()
{
//生成符合均值为10,标准差为2的随机数
normal_distribution<double> u(10, 2);
default_random_engine e(time(NULL));
for (size_t i = 1; i <= 100; ++i)
{
printf("%-9.6lf ", u(e));
if (i % 10 == 0)
{
cout << endl;
}
}
cout << endl;
return 0;
}
class bernoulli_distribution;
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
int main()
{
// 生成1的概率为0.7
bernoulli_distribution u(0.7);
default_random_engine e(time(NULL));
for (int i = 0; i < 10; i++) {
cout << u(e) << " ";
}
cout << endl;
return 0;
}
以上就是“C++怎么生成随机浮点数”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。