In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
今天小编给大家分享一下C++怎么生成随机浮点数的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
rand生成随机数问题出现
场景描述:
想生成一组整形随机数,放入数组中,用来测试自己的排序是否正确。
于是我写出了下方代码,生成随机数。
先简单了解下用到的函数:
//返回time_t类型的 当前时间的时间戳time_t time (time_t* timer);//传入一个种子,为伪随机数生成器初始化void srand (unsigned int seed);//得到一个整形伪随机数int rand (void);#include #include #include 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 #include #include #include 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 #include #include 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 uniform_int_distribution;// IntType// An integer type. Aliased as member type result_type.// By default, this is int.#include #include #include using namespace std;int main(){ //产生[1, 100]左闭右闭区间的随机整数 uniform_int_distribution 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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 294
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.