目录
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
https://nolvadex.life/# tamoxifen lawsuit
tamoxifen medication: aromatase inhibitors tamoxifen – tamoxifen citrate
https://lisinopril.network/# lisinopril 2.5 mg price
order cytotec online: cytotec buy online usa – cytotec buy online usa
should i take tamoxifen does tamoxifen cause menopause nolvadex vs clomid
mexican mail order pharmacies: Online Pharmacies in Mexico – п»їbest mexican online pharmacies
http://indiaph24.store/# best online pharmacy india
india pharmacy pharmacy website india india online pharmacy
buying from online mexican pharmacy: cheapest mexico drugs – mexican pharmaceuticals online
http://mexicoph24.life/# mexican drugstore online
online canadian pharmacy review Licensed Canadian Pharmacy best online canadian pharmacy
https://canadaph24.pro/# canadian pharmacy 1 internet online drugstore
buying prescription drugs in mexico: Online Pharmacies in Mexico – reputable mexican pharmacies online
https://indiaph24.store/# indianpharmacy com
http://indiaph24.store/# top online pharmacy india
pharmacies in mexico that ship to usa Mexican Pharmacy Online buying prescription drugs in mexico online
http://mexicoph24.life/# mexican online pharmacies prescription drugs
http://mexicoph24.life/# mexican pharmaceuticals online
https://indiaph24.store/# legitimate online pharmacies india
top 10 online pharmacy in india: indian pharmacy fast delivery – buy medicines online in india
http://mexicoph24.life/# mexican pharmaceuticals online
https://mexicoph24.life/# mexico pharmacy
https://canadaph24.pro/# legit canadian online pharmacy
http://indiaph24.store/# india online pharmacy
https://indiaph24.store/# best india pharmacy
reputable indian online pharmacy: Cheapest online pharmacy – top 10 pharmacies in india
https://indiaph24.store/# indian pharmacy paypal
https://indiaph24.store/# mail order pharmacy india
http://canadaph24.pro/# cheapest pharmacy canada
indian pharmacy online Cheapest online pharmacy indian pharmacy online
http://mexicoph24.life/# mexico drug stores pharmacies
http://canadaph24.pro/# safe online pharmacies in canada
https://indiaph24.store/# top online pharmacy india
best canadian pharmacy to order from: Licensed Canadian Pharmacy – 77 canadian pharmacy
http://mexicoph24.life/# medication from mexico pharmacy
http://canadaph24.pro/# cheap canadian pharmacy
https://mexicoph24.life/# buying prescription drugs in mexico online
https://canadaph24.pro/# canadian world pharmacy
https://mexicoph24.life/# purple pharmacy mexico price list
mexican online pharmacies prescription drugs: mexico pharmacy – buying prescription drugs in mexico online
https://indiaph24.store/# india pharmacy mail order
http://mexicoph24.life/# medicine in mexico pharmacies
https://canadaph24.pro/# canada pharmacy online legit
https://mexicoph24.life/# purple pharmacy mexico price list
http://mexicoph24.life/# buying prescription drugs in mexico
medication from mexico pharmacy mexico pharmacy medication from mexico pharmacy
http://canadaph24.pro/# canadian pharmacy in canada
medication from mexico pharmacy: cheapest mexico drugs – mexican mail order pharmacies
https://mexicoph24.life/# purple pharmacy mexico price list
https://canadaph24.pro/# legit canadian pharmacy