目录
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
п»їfarmacia online espaГ±a: farmacia online barata – farmacia barata
internet apotheke: internet apotheke – internet apotheke
trouver un médicament en pharmacie: pharmacie en ligne sans ordonnance – pharmacie en ligne france
pharmacie en ligne france livraison belgique Pharmacie sans ordonnance trouver un mГ©dicament en pharmacie
farmacia online senza ricetta: farmacie online autorizzate elenco – farmacia online senza ricetta
farmacie online autorizzate elenco: farmacia online senza ricetta – top farmacia online
https://eufarmacieonline.shop/# farmacia online senza ricetta
farmacia online envГo gratis farmacia online 24 horas farmacia online espaГ±a envГo internacional
The grace and authority you handle topics with are as mesmerizing as a moonlit dance. I’m thoroughly impressed.
farmacia online barcelona: farmacia online espaГ±a envГo internacional – п»їfarmacia online espaГ±a
farmacie online autorizzate elenco: Farmacia online miglior prezzo – farmacie online autorizzate elenco
Farmacia online miglior prezzo: farmacie online autorizzate elenco – farmacie online autorizzate elenco
pharmacies en ligne certifiées: pharmacie en ligne france fiable – pharmacie en ligne
Achat mГ©dicament en ligne fiable acheter mГ©dicament en ligne sans ordonnance pharmacies en ligne certifiГ©es
top farmacia online: п»їFarmacia online migliore – farmacia online piГ№ conveniente
comprare farmaci online con ricetta: Farmacie on line spedizione gratuita – farmacia online
farmacias online seguras en espaГ±a farmacia online madrid farmacias direct
https://euapothekeohnerezept.com/# online apotheke
п»їFarmacia online migliore: comprare farmaci online con ricetta – п»їFarmacia online migliore
online apotheke deutschland: eu apotheke ohne rezept – internet apotheke
farmacie online autorizzate elenco farmacie online autorizzate elenco acquistare farmaci senza ricetta
pharmacie en ligne avec ordonnance: pharmacie en ligne pas cher – Pharmacie Internationale en ligne
pharmacie en ligne france fiable: pharmacie en ligne pas cher – pharmacie en ligne france fiable
farmacias online seguras: farmacias online seguras – farmacias direct
farmacia online senza ricetta: farmacia online piГ№ conveniente – comprare farmaci online all’estero
farmacias online seguras en espaГ±a farmacia online barata y fiable farmacias online seguras en espaГ±a
online apotheke: gГјnstigste online apotheke – medikamente rezeptfrei
farmacia online 24 horas: farmacia barata – farmacia online envÃo gratis
https://euapothekeohnerezept.com/# medikament ohne rezept notfall
apotheke online online apotheke rezept medikament ohne rezept notfall
Farmacia online miglior prezzo: top farmacia online – farmaci senza ricetta elenco
pharmacie en ligne fiable: pharmacie en ligne france pas cher – pharmacie en ligne france livraison internationale
eu apotheke ohne rezept: apotheke online – apotheke online
http://cheapestmexico.com/# medicine in mexico pharmacies
http://cheapestandfast.com/# canada pharmacy no prescription
https://cheapestandfast.shop/# best online pharmacy that does not require a prescription in india
best online canadian pharmacy canadian world pharmacy canadian drug pharmacy
https://cheapestcanada.shop/# escrow pharmacy canada
https://cheapestmexico.shop/# п»їbest mexican online pharmacies
https://cheapestindia.com/# top 10 online pharmacy in india
canadian pharmacy world coupon: 36 & 6 health – us pharmacy no prescription
https://cheapestcanada.shop/# online canadian drugstore
canadian pharmacy discount coupon 36 and 6 health online pharmacy mail order prescription drugs from canada
http://36and6health.com/# canadian pharmacy coupon
http://36and6health.com/# cheap pharmacy no prescription
https://cheapestmexico.com/# buying from online mexican pharmacy
canadian pharmacy meds: cheapestcanada.com – best canadian online pharmacy
mexican rx online mexican rx online mexican border pharmacies shipping to usa
http://cheapestandfast.com/# online drugs no prescription
https://cheapestcanada.com/# best canadian pharmacy