目录
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://casinvietnam.shop/# choi casino tr?c tuy?n tren di?n tho?i
casino tr?c tuy?n vi?t nam: casino tr?c tuy?n – casino tr?c tuy?n
https://casinvietnam.shop/# choi casino tr?c tuy?n tren di?n tho?i
https://edpill.top/# get ed meds today
https://medicationnoprescription.pro/# no prescription drugs online
canadian prescription pharmacy: pharmacy without prescription – promo code for canadian pharmacy meds
cheapest ed treatment cheapest ed treatment low cost ed pills
ed medication online: online ed pharmacy – where can i get ed pills
http://medicationnoprescription.pro/# medications online without prescriptions
rx pharmacy coupons: cheapest prescription pharmacy – us pharmacy no prescription
http://medicationnoprescription.pro/# indian pharmacy no prescription
http://edpill.top/# п»їed pills online
https://edpill.top/# ed treatment online
http://medicationnoprescription.pro/# buying prescription drugs online without a prescription
canadian mail order prescriptions: mexico online pharmacy prescription drugs – pharmacy no prescription
canada prescription canadian pharmacy no prescription needed prescription canada
Centre at M비아그라 구매andapam, Tam비아그라 구매il Nadu (one비아그라 구매 of
http://onlinepharmacyworld.shop/# canadian pharmacy discount coupon
no prescription needed canadian pharmacy: pharmacy no prescription required – canadian pharmacy no prescription needed
cheapest pharmacy for prescription drugs: pharmacy coupons – cheapest pharmacy to fill prescriptions with insurance
http://edpill.top/# get ed prescription online
cheapest online ed meds how to get ed pills erection pills online
http://edpill.top/# cheap ed medicine
medications online without prescriptions: online pharmacy no prescriptions – canadian prescriptions in usa
http://edpill.top/# ed drugs online
Wow, wonderful weblog format! How lengthy have you been blogging for?
you make running a blog look easy. The overall look of your site is
excellent, as well as the content material! You can see similar here najlepszy sklep
http://onlinepharmacyworld.shop/# pharmacy coupons
prednisone 30 mg: where can i buy prednisone without a prescription – prednisone for sale without a prescription
azithromycin amoxicillin: amoxicillin 500mg capsule buy online – order amoxicillin online uk
http://clomida.pro/# can i order generic clomid now
zithromax 250 mg tablet price: can you buy zithromax over the counter in mexico – buy zithromax 500mg online
Hey! This is my first visit to your blog! We are a team of volunteers and starting a new project in a community
in the same niche. Your blog provided us useful information to work on.
You have done a extraordinary job!
where can i get generic clomid pills: can you get clomid for sale – can you buy clomid for sale
amoxicillin 500 mg where to buy amoxicillin 500mg tablets price in india amoxicillin 250 mg capsule
https://clomida.pro/# can i buy generic clomid without rx
buy zithromax canada: purchase zithromax online – where can i get zithromax
It’s hard to find educated people for this subject, but you sound like you know what you’re talking about!
Thanks
can i purchase generic clomid tablets: can you buy generic clomid for sale – can i get generic clomid without rx
cost of clomid without a prescription buy cheap clomid prices can i get clomid without insurance
https://azithromycina.pro/# zithromax for sale us
ll these ser비아그라 구매ies followed비아그라 구매 the same st비아그라 구매yle
ivermectin 3mg: stromectol xr – buy ivermectin stromectol
http://stromectola.top/# minocycline 100mg without doctor
canine prednisone 5mg no prescription: prednisone 10mg tablets – prednisone steroids
http://prednisonea.store/# prednisone without rx
ivermectin lotion cost stromectol order stromectol pills
zithromax generic price: zithromax order online uk – zithromax coupon
ivermectin 1%: cost of ivermectin pill – buy ivermectin nz
where to buy generic clomid without dr prescription: clomid pills – order clomid without dr prescription
ivermectin cream 5% ivermectin medication stromectol 0.5 mg