目录
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
Cheap Viagra 100mg viagra without prescription over the counter sildenafil
cheapest cenforce: buy cenforce – Buy Cenforce 100mg Online
https://cenforce.pro/# cheapest cenforce
https://kamagra.win/# buy kamagra online usa
Cheap generic Viagra: Buy Viagra online – cheapest viagra
Cialis 20mg price in USA Cialis 20mg price in USA Cheap Cialis
Buy Vardenafil 20mg: levitrav.store – Buy generic Levitra online
http://cenforce.pro/# cenforce for sale
buy cialis pill cialist.pro Cheap Cialis
cheapest cenforce: order cenforce – Cenforce 150 mg online
http://viagras.online/# Sildenafil Citrate Tablets 100mg
cheapest cenforce cenforce for sale order cenforce
http://cialist.pro/# Cialis 20mg price
http://viagras.online/# Cheap generic Viagra online
cenforce for sale: buy cenforce – order cenforce
cheapest cialis: Generic Tadalafil 20mg price – Buy Tadalafil 20mg
Order Viagra 50 mg online Viagra online price Order Viagra 50 mg online
https://viagras.online/# buy viagra here
cheapest cialis: Cialis 20mg price in USA – Cialis without a doctor prescription
Cialis over the counter Cialis 20mg price in USA п»їcialis generic
http://ciprofloxacin.tech/# ciprofloxacin 500mg buy online
tamoxifen men nolvadex pills tamoxifen endometriosis
https://nolvadex.life/# nolvadex online
buy cytotec pills online cheap: Cytotec 200mcg price – buy cytotec pills online cheap
http://nolvadex.life/# tamoxifen vs raloxifene
ciprofloxacin generic buy cipro online antibiotics cipro
buy cipro online without prescription: where can i buy cipro online – buy cipro cheap
https://ciprofloxacin.tech/# cipro for sale
http://cytotec.club/# cytotec buy online usa
buy ciprofloxacin over the counter buy cipro buy cipro online
home: cheap propecia prices – propecia brand name
http://finasteride.store/# cost of cheap propecia without rx
buy lisinopril 20 mg lisinopril 10 mg lisinopril from mexico
get propecia tablets: propecia cheap – cheap propecia without prescription
cost of propecia without rx propecia for sale propecia tablet
http://nolvadex.life/# tamoxifen joint pain
https://lisinopril.network/# lisinopril 500 mg
lisinopril 20mg tablets cost: lisinopril canada – generic lisinopril 5 mg
ciprofloxacin mail online cipro 500mg best prices buy cipro online without prescription
https://ciprofloxacin.tech/# buy cipro online without prescription
propecia cost: order propecia without prescription – get cheap propecia without dr prescription
purchase cipro cipro 500mg best prices cipro for sale
http://ciprofloxacin.tech/# п»їcipro generic
http://cytotec.club/# buy cytotec pills
buying propecia without insurance: cost of generic propecia for sale – get propecia
lisinopril 5 mg prinivil brand name lisinopril 5 mg brand name in india
https://ciprofloxacin.tech/# ciprofloxacin 500mg buy online
buy cytotec over the counter: buy cytotec pills – buy cytotec in usa
https://lisinopril.network/# 25 mg lisinopril
tamoxifen moa nolvadex 10mg tamoxifen alternatives