C ++ various problems with pseudo random

xiaoxiao2021-03-05  23

1. Basic problem, how to generate a random number of [0..100] #include

#include

#include

INT main () {srand (null); for (int i = 0; i <10; i ) printf ("% D / T", RAND ()% 100); return 0;} This is a pseudo random number, I did experiment, under 10,000 samples, a bit distance from 10.00% of the uniform distribution, and it is already very good under 1 million. Use standard C . 2. How to random produce a string with 6 digits (such as password, verification code)? There are two ways: char password [7]; INT I, J; SRAND (Time (0)); for (i = 0; i <6; i ) {j = rand ()% 10; Password [i] = J '0';} Password [6] = '{row.content}'; another method: int password; INT I, J; SRAND (Time (0)); for (i = 0; i <6; i ) {j = rand ()% 10; password = j password * 10;} // then convert the int password to the city string, COUT <

<

#include

#include

#include

Int main () {std :: vector

IVEC (0, 100); for (int i = 0; i <100; i IVEC.PUSH_BACK (i 1); std :: copy (IVEC.BEGIN (), IVEC.END (), std :: Ostream_IstRerator

(std :: cout, "; std :: cout <<" / n / n "; std :: vector

:: Iterator it = IVEC.BEGIN (); std :: Vector

:: Iterator IT_END = IVEC.END (); std :: random_shuffle (IVEC.BEGIN (), IVEC.End (); std :: copy (it, it_end, std :: ostream_iterator

(std :: cout, ""; return 0;} 4. Come again, how to generate a random number of normal distribution: / * Generally there are two algorithms: an algorithm generates 12 (0,1) average distribution random The function can simulate a normal distribution with a large number of theorem. The algorithm two uses the Jacobi transform in mathematics to generate a normal distribution, but this algorithm will overflow errors in calculating a large number of times. Test procedure: * / #include

#include

#include

#include

#include

转载请注明原文地址:https://www.9cbs.com/read-36054.html

New Post(0)