目录
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
prednisone 30 mg daily: prednisone 50 mg buy – prednisone 54899
neurontin cost in singapore neurontin online usa neurontin 400 mg capsule
https://doxycyclinea.online/# doxy 200
buy zithromax without presc: zithromax coupon – zithromax
order prednisone 10mg: 40 mg daily prednisone – prednisone 5mg over the counter
neurontin 800 mg: neurontin 800 mg tablets – neurontin 100mg
amoxicillin 500 mg purchase without prescription purchase amoxicillin online without prescription amoxicillin 500 tablet
https://zithromaxa.store/# generic zithromax 500mg
how much is prednisone 10mg: where can i buy prednisone without prescription – prednisone 4mg tab
how to order doxycycline doxycycline medication buy doxycycline 100mg
https://doxycyclinea.online/# buy doxycycline online 270 tabs
buy neurontin uk neurontin 600 mg capsule neurontin
can you buy amoxicillin over the counter in canada: 875 mg amoxicillin cost – where to get amoxicillin over the counter
prednisone 5093: buy 40 mg prednisone – how to get prednisone tablets
amoxicillin 825 mg: amoxicillin 500 mg brand name – order amoxicillin online no prescription
gabapentin 300mg: neurontin 600 mg price – neurontin rx
amoxicillin without prescription: order amoxicillin 500mg – purchase amoxicillin 500 mg
https://amoxila.pro/# azithromycin amoxicillin
neurontin 330 mg: buy neurontin 100 mg canada – drug neurontin
prednisone where can i buy: buy prednisone 1 mg mexico – prednisone 40mg
medicine neurontin capsules where can i buy neurontin online neurontin medication
prednisone where can i buy: prednisone without prescription – 40 mg daily prednisone
https://gabapentinneurontin.pro/# buy neurontin 100 mg canada
amoxicillin over the counter in canada amoxicillin price without insurance amoxicillin 800 mg price
buying prescription drugs in canada: online pharmacy canada no prescription – buying prescription medications online
mexican drugstore online: mexican online pharmacies prescription drugs – mexican pharmaceuticals online
http://pharmmexico.online/# mexican online pharmacies prescription drugs
canadian prescription drugstore reviews canadian pharmacy online no prescription buy prescription drugs online without doctor
drugs from canada: canadian pharmacy reviews – recommended canadian pharmacies
https://pharmnoprescription.icu/# online canadian pharmacy no prescription
indian pharmacies safe indianpharmacy com top online pharmacy india
best website to buy prescription drugs: buying drugs without prescription – canadian pharmacy no prescription needed
mexican mail order pharmacies: mexican mail order pharmacies – п»їbest mexican online pharmacies
indianpharmacy com: best india pharmacy – best online pharmacy india
https://pharmindia.online/# indian pharmacy paypal
canadian pharmacy discount code canadian pharmacy world coupons cheapest pharmacy to get prescriptions filled
canadian pharmacy world reviews: reliable canadian pharmacy reviews – pharmacy canadian superstore
https://pharmmexico.online/# best online pharmacies in mexico
canadianpharmacymeds com ed meds online canada canadian discount pharmacy
meds online no prescription: canadian rx prescription drugstore – pharmacy with no prescription
top 10 pharmacies in india: top online pharmacy india – indian pharmacy
buy prescription drugs from canada cheap canadian pharmacy 77 canadian pharmacy
http://pharmcanada.shop/# online canadian pharmacy
http://pharmnoprescription.icu/# canada prescription drugs online
mexican border pharmacies shipping to usa: mexican online pharmacies prescription drugs – buying prescription drugs in mexico online
mexico drug stores pharmacies mexico pharmacy mexico pharmacy
Viagra tablet online Buy Viagra online cheap best price for viagra 100mg
sildenafil oral jelly 100mg kamagra: kamagra pills – Kamagra tablets
https://kamagra.win/# Kamagra 100mg
http://kamagra.win/# Kamagra tablets