About random functions

xiaoxiao2021-03-06  41

When writing a program, you will often encounter a place with a random function. Here I give a simple way, similar things in MSDN. To create a true random number, it is more complicated, but it is not necessary for most applications.

Sample code:

// name: rand_example.cpp // compile: CL / EHSC RAND_EXAMPLE.CPP / / OS: Window 2000 or Later

#include #include #include #include

Template struct display {void operator () (const t value) {std :: cout << value << ';}};

Inline int random (int min) {return ((rand ()% (MAX 1-min)) min);

Int main () {typedef st :: vector Intarray; INTARRAY MyArray;

SRAND (NULL);

For (int i = 0; i <10; i) MyArray.push_back (Random (10, 60));

FOR_EACH (MyArray.begin (), MyArray.end (), Display ());

Return 0;}

In the above program, the number of random numbers can be obtained, and if the precision is high, consider using Win32 API QueryPerFormanceCounter to obtain higher precision time values ​​as a seed value.

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

New Post(0)