目录
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
zithromax online: can you buy zithromax online – zithromax 500
buy prednisone online australia buy prednisone 20mg prednisone 50mg cost
https://azithromycina.pro/# zithromax online usa no prescription
prednisone for sale without a prescription: 10 mg prednisone – prednisone for sale no prescription
zithromax buy: zithromax online pharmacy canada – zithromax azithromycin
https://nolvadex.icu/# tamoxifen citrate
п»їcytotec pills online: cytotec abortion pill – buy cytotec over the counter
tamoxifen and antidepressants: lexapro and tamoxifen – tamoxifen endometrium
tamoxifen and depression where to buy nolvadex tamoxifen bone pain
buy cytotec in usa: Misoprostol 200 mg buy online – cytotec pills online
diflucan 6 tabs: diflucan online – diflucan tablets buy online no script
nolvadex for sale amazon tamoxifen chemo tamoxifen citrate
where to purchase doxycycline: vibramycin 100 mg – order doxycycline
buy doxycycline online 270 tabs: buy doxycycline 100mg – generic for doxycycline
nolvadex steroids: aromatase inhibitors tamoxifen – tamoxifen menopause
as been desi비아그라효능gnated as a 비아그라효능Living Natio비아그라효능nal
cytotec abortion pill buy cytotec over the counter buy cytotec over the counter
https://doxycyclinest.pro/# doxycycline generic
diflucan over the counter: diflucan generic price – where to get diflucan
price of doxycycline: doxycycline 100mg capsules – doxycycline generic
200 mg doxycycline doxycycline 100mg capsules doxycycline hydrochloride 100mg
ciprofloxacin generic: ciprofloxacin generic price – buy ciprofloxacin over the counter
doxycycline generic: doxycycline vibramycin – price of doxycycline
cost of diflucan over the counter ordering diflucan generic diflucan for sale
order cytotec online: buy cytotec – cytotec pills online
can i buy diflucan over the counter in australia: diflucan 6 tablets – diflucan 150 mg tablet
buy diflucan generic: diflucan 400mg without prescription – diflucan 100 mg
ordering diflucan generic: can you buy diflucan in mexico – medicine diflucan price
buy cytotec online fast delivery cytotec online buy misoprostol over the counter
r coating. T비아그라가격he coating i비아그라가격s flexible (비아그라가격coul
http://misoprostol.top/# п»їcytotec pills online
doxycycline tablets: buy doxycycline – doxycycline hyc
cytotec pills buy online buy cytotec in usa Cytotec 200mcg price
ciprofloxacin order online: buy cipro online – ciprofloxacin mail online
buy doxycycline online: generic for doxycycline – buy doxycycline without prescription uk
diflucan for sale uk purchase diflucan online diflucan capsule price
buy cytotec over the counter: buy cytotec in usa – buy cytotec pills
buy cytotec online: purchase cytotec – Abortion pills online
п»їcytotec pills online п»їcytotec pills online buy cytotec pills
buy nolvadex online: tamoxifen for sale – tamoxifen reviews
The “The Wea비아그라구입lth of India비아그라구입 — Raw Mater비아그라구입ials
buy cytotec pills online cheap: cytotec abortion pill – buy cytotec online fast delivery
https://misoprostol.top/# buy cytotec online
ciprofloxacin 500mg buy online: cipro pharmacy – ciprofloxacin 500 mg tablet price
buy diflucan uk diflucan tabs diflucan 6
nolvadex 20mg: alternatives to tamoxifen – tamoxifen depression
purchase cytotec order cytotec online cytotec pills buy online
buy cytotec pills: buy cytotec online – buy cytotec over the counter
tamoxifen chemo: tamoxifen dosage – nolvadex side effects
buy cipro online without prescription: buy ciprofloxacin over the counter – cipro online no prescription in the usa