目录
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
medicine in mexico pharmacies mexican pharmacy purple pharmacy mexico price list
https://mexicanpharmacy1st.online/# medicine in mexico pharmacies
mexican rx online: reputable mexican pharmacies online – buying prescription drugs in mexico
mexican pharmaceuticals online best online pharmacies in mexico mexico pharmacies prescription drugs
https://mexicanpharmacy1st.com/# reputable mexican pharmacies online
reputable mexican pharmacies online: medicine in mexico pharmacies – mexico drug stores pharmacies
https://mexicanpharmacy1st.shop/# mexican pharmacy
mexico pharmacies prescription drugs: mexico drug stores pharmacies – mexico drug stores pharmacies
mexican border pharmacies shipping to usa: mexican drugstore online – reputable mexican pharmacies online
mexico drug stores pharmacies pharmacies in mexico that ship to usa mexico pharmacies prescription drugs
https://mexicanpharmacy1st.com/# best online pharmacies in mexico
pharmacies in mexico that ship to usa: mexico pharmacies prescription drugs – medicine in mexico pharmacies
pharmacies in mexico that ship to usa reputable mexican pharmacies online п»їbest mexican online pharmacies
amoxicillin 500mg buy online uk: amoxicillin online pharmacy – amoxicillin script
http://gabapentinneurontin.pro/# neurontin 500 mg
generic neurontin 300 mg neurontin 100 mg cost neurontin gabapentin
prednisone rx coupon: buy prednisone online paypal – buy prednisone with paypal canada
zithromax capsules australia: zithromax coupon – zithromax online usa
generic prednisone tablets: prednisone 10 mg daily – cost of prednisone 5mg tablets
https://doxycyclinea.online/# where can i get doxycycline
prednisone 20mg cheap prednisone for dogs prednisone 10mg tabs
canine prednisone 5mg no prescription: 400 mg prednisone – prednisone 40 mg daily
zithromax 500 generic zithromax medicine buy zithromax online cheap
http://prednisoned.online/# medicine prednisone 10mg
amoxicillin generic brand: cost of amoxicillin prescription – amoxicillin 500
amoxicillin 875 125 mg tab: generic amoxicillin over the counter – buy amoxicillin online mexico
amoxicillin 750 mg price: generic amoxicillin cost – buy amoxicillin without prescription
prednisone rx coupon online prednisone 5mg fast shipping prednisone
http://gabapentinneurontin.pro/# buy neurontin uk
doxycycline: doxycycline 100mg – doxy
neurontin 300 mg buy neurontin 150 mg neurontin india
https://amoxila.pro/# order amoxicillin no prescription
doxycycline order online: buy cheap doxycycline – buy cheap doxycycline
doxycycline hydrochloride 100mg doxycycline tetracycline doxycycline 200 mg
prednisone buy no prescription: prednisone 10 mg online – prednisone 10mg
http://zithromaxa.store/# zithromax order online uk
doxycycline hyclate 100 mg cap: doxycycline tablets – doxy
zithromax coupon: zithromax 1000 mg online – zithromax 500mg price in india
cheap amoxicillin 500mg amoxicillin 250 mg capsule amoxicillin canada price
http://doxycyclinea.online/# doxycycline without a prescription
neurontin medication: neurontin for sale online – gabapentin buy
neurontin 400 mg tablets cost of neurontin 600 mg neurontin online usa
https://zithromaxa.store/# zithromax cost uk
prednisone capsules: prednisone price south africa – prednisone 20 mg tablet price
where can i get doxycycline: doxycycline 100mg online – how to order doxycycline
amoxicillin 30 capsules price: azithromycin amoxicillin – cost of amoxicillin 30 capsules
doxycycline 500mg buy cheap doxycycline online doxycycline monohydrate
http://doxycyclinea.online/# doxy 200
neurontin 100mg tablet: neurontin capsule 600mg – neurontin 100 mg tablets
http://doxycyclinea.online/# doxycycline 100mg price