目录
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://gabapentin.club/# neurontin 2018
neurontin 800 mg cost neurontin prescription cost cost of neurontin 100mg
propecia price: buying generic propecia – buy generic propecia no prescription
how to get clomid: can i get generic clomid without insurance – cost of cheap clomid tablets
gabapentin gabapentin buy neurontin 600mg
medication neurontin 300 mg: neurontin 4 mg – order neurontin
gabapentin 300mg: neurontin online usa – neurontin 600 mg
http://gabapentin.club/# gabapentin 300mg
http://lisinopril.club/# lisinopril 1 mg tablet
lisinopril 2.5 mg medicine lisinopril sale lisinopril tab 100mg
can i order generic clomid without insurance: cost of generic clomid without insurance – clomid pill
http://clomiphene.shop/# how to buy clomid for sale
where can i get clomid without rx: cost of clomid no prescription – clomid brand name
lisinopril 40 lisinopril 20mg tablets price prinzide zestoretic
cost of generic propecia pills: generic propecia without prescription – cost cheap propecia price
https://lisinopril.club/# lisinopril 5 mg buy
buy cytotec pills online cheap buy cytotec п»їcytotec pills online
http://clomiphene.shop/# where can i get cheap clomid price
buy neurontin 300 mg: prescription drug neurontin – gabapentin online
http://cytotec.xyz/# buy cytotec over the counter
order propecia without rx propecia no prescription buying propecia without insurance
price of zestril 30 mg: can i buy lisinopril over the counter in mexico – lisinopril 2 5 mg tablets
no prescription lisinopril lisinopril 10 mg prices zestril 10 mg online
buy cheap lisinopril: lisinopril 12.5 mg 20 mg – lisinopril 60 mg
http://lisinopril.club/# lisinopril 40 mg price
cytotec abortion pill: order cytotec online – buy cytotec online fast delivery
cytotec buy online usa buy cytotec pills online cheap buy cytotec pills online cheap
https://gabapentin.club/# neurontin tablets 100mg
https://mexicanpharmacy1st.com/# purple pharmacy mexico price list
buying prescription drugs in mexico online mexico drug stores pharmacies purple pharmacy mexico price list
mexican rx online: best online pharmacies in mexico – best mexican online pharmacies
http://mexicanpharmacy1st.com/# mexican drugstore online
buying prescription drugs in mexico online medication from mexico pharmacy best online pharmacies in mexico
mexico pharmacies prescription drugs: reputable mexican pharmacies online – mexican drugstore online
https://mexicanpharmacy1st.online/# mexico pharmacy
https://mexicanpharmacy1st.shop/# purple pharmacy mexico price list
purple pharmacy mexico price list: mexican drugstore online – mexico drug stores pharmacies
purple pharmacy mexico price list buying prescription drugs in mexico online medication from mexico pharmacy
buying prescription drugs in mexico: medicine in mexico pharmacies – mexican online pharmacies prescription drugs
https://mexicanpharmacy1st.shop/# medicine in mexico pharmacies
mexico drug stores pharmacies: reputable mexican pharmacies online – medication from mexico pharmacy
pharmacies in mexico that ship to usa medicine in mexico pharmacies buying prescription drugs in mexico
best online pharmacies in mexico: mexican online pharmacies prescription drugs – pharmacies in mexico that ship to usa
http://mexicanpharmacy1st.com/# reputable mexican pharmacies online
mexican mail order pharmacies mexico drug stores pharmacies purple pharmacy mexico price list
mexico drug stores pharmacies: mexico drug stores pharmacies – mexico drug stores pharmacies
https://mexicanpharmacy1st.shop/# medication from mexico pharmacy
https://mexicanpharmacy1st.com/# mexico pharmacy
buying prescription drugs in mexico: pharmacies in mexico that ship to usa – buying prescription drugs in mexico
buying from online mexican pharmacy: reputable mexican pharmacies online – mexico pharmacy