目录
rand()
在以往的c/c++中生成随机数是使用rand()函数,但它也有一些使用的注意事项
头文件
#include <stdlib.h>
使用
随机数范围
- rand()的返回值范围在 [0, RAND_MAX]
- RAND_MAX是一个宏定义,在stdlib.h中,在部分系统中值为32768
- 如果需要其他范围,则需要自己运算,例如:
- [a,b) 的随机整数,使用 (rand() % (b-a))+ a;
- [a,b] 的随机整数,使用 (rand() % (b-a+1))+ a;
- (a,b] 的随机整数,使用 (rand() % (b-a))+ a + 1;
需要srand()初始化种子
- void srand(unsigned int seed);
- 如果不使用srand()初始化,然后直接调用rand()的话,会自动调用srand(1)进行初始化,因此你会发现,将写好的程序重复运行多次,每次生成的随机数是一样的。
- 我们一般是使用时间戳进行初始化,如:
#include <time.h>
#include <stdlib.h>
int main() {
srand(time(nullptr)); //初始化种子
rand(); //获取随机数
return 0;
}
多线程时
- 在多线程中,对于rand()来说每个线程都是独立的,因此需要在每一个需要生成随机数的线程都进行初始化。
- 比如,假设程序现在有线程 main, a, b, c
- 其中main, a线程各自调用了srand(time(nullptr));进行初始化种子,而b,c线程没有
- 那么main,a线程中调用rand()才能得到不同随机数
- 而在b, c中,会默认调用srand(1),因此这俩线程中生成的随机数是一样的。
c++11_random
- 如果你可以使用c++11标准,应该考虑使用<random>库的新方法获取随机数,功能更强更省心,他们的使用方法需要再了解:
- 产生均匀分布的整数:uniform_int_distribution
- 产生均匀分布的实数:uniform_real_distribution
- 产生正态分布的实数:normal_distribution
- 生成二项分布的布尔值:bernoulli_distribution
http://canadaph24.pro/# canada ed drugs
http://indiaph24.store/# indian pharmacies safe
http://canadaph24.pro/# canadian 24 hour pharmacy
http://indiaph24.store/# online shopping pharmacy india
http://indiaph24.store/# indian pharmacies safe
legitimate canadian pharmacies Licensed Canadian Pharmacy canadian pharmacy online ship to usa
https://mexicoph24.life/# mexican border pharmacies shipping to usa
reputable indian pharmacies Cheapest online pharmacy Online medicine order
canadian pharmacy tampa: Large Selection of Medications from Canada – canadian discount pharmacy
http://indiaph24.store/# mail order pharmacy india
http://canadaph24.pro/# cheapest pharmacy canada
http://canadaph24.pro/# canada cloud pharmacy
http://indiaph24.store/# india pharmacy
top 10 pharmacies in india: Cheapest online pharmacy – india online pharmacy
https://indiaph24.store/# best india pharmacy
http://canadaph24.pro/# canadian discount pharmacy
http://indiaph24.store/# online pharmacy india
http://canadaph24.pro/# buy prescription drugs from canada cheap
https://indiaph24.store/# buy prescription drugs from india
buying prescription drugs in mexico online: Online Pharmacies in Mexico – purple pharmacy mexico price list
https://indiaph24.store/# reputable indian pharmacies
https://canadaph24.pro/# the canadian pharmacy
http://canadaph24.pro/# canadian discount pharmacy
http://canadaph24.pro/# ed meds online canada
https://indiaph24.store/# top 10 pharmacies in india
canadian drugs Certified Canadian Pharmacies canadian pharmacies online
https://indiaph24.store/# reputable indian pharmacies
http://indiaph24.store/# mail order pharmacy india
https://indiaph24.store/# mail order pharmacy india
http://mexicoph24.life/# mexican pharmacy
п»їlegitimate online pharmacies india indian pharmacy fast delivery india pharmacy
https://indiaph24.store/# online pharmacy india
https://indiaph24.store/# online shopping pharmacy india
http://indiaph24.store/# reputable indian pharmacies
Online medicine order indian pharmacy fast delivery indianpharmacy com
http://indiaph24.store/# online pharmacy india
buy canadian drugs: Certified Canadian Pharmacies – thecanadianpharmacy
https://indiaph24.store/# indian pharmacies safe
choi casino tr?c tuy?n trên di?n tho?i: dánh bài tr?c tuy?n – dánh bài tr?c tuy?n
choi casino tr?c tuy?n trên di?n tho?i: casino tr?c tuy?n uy tín – casino tr?c tuy?n vi?t nam
casino tr?c tuy?n vi?t nam: casino tr?c tuy?n uy tín – casino tr?c tuy?n
casino tr?c tuy?n uy tín: web c? b?c online uy tín – casino tr?c tuy?n uy tín
https://casinvietnam.shop/# casino online uy tin
casino tr?c tuy?n vi?t nam casino tr?c tuy?n vi?t nam casino tr?c tuy?n uy tin
http://casinvietnam.com/# game c? b?c online uy tin
choi casino tr?c tuy?n tren di?n tho?i casino tr?c tuy?n uy tin casino tr?c tuy?n
http://casinvietnam.com/# web c? b?c online uy tin
casino online uy tín: web c? b?c online uy tín – casino tr?c tuy?n vi?t nam
https://casinvietnam.com/# choi casino tr?c tuy?n tren di?n tho?i
casino tr?c tuy?n choi casino tr?c tuy?n tren di?n tho?i casino tr?c tuy?n uy tin