What is the most random function in C ++?

I used

#include<stdlib>
#include<time>
using namespace std;
srand((unsigned)time(0));
int n=(rand()>>8)%4;

but what other random functions are there or what other function can be used as random number generators?

EDIT: I have no particular reason to ask this question, I just wanted to know if C ++ has any other random functions.

+5
source share
9 answers

The rand () and srand () functions are all that is specified in the C ++ standard. And if it comes to writing your own, remember what John von Neumann said:

"Anyone who considers arithmetic methods to create random numbers of course, in a state of sin"

+14
source
+16

. .

int FastRandom()
{
  return 10;
}
+14

++, Windows :

CryptGenRandom

, .

+5
int unixrand()
{
   int x;
   int f = open("/dev/random", O_RDONLY);
   if (f < 0) return -1; /* Error */
   if (sizeof(x) != read(f, &x, sizeof(x))) {
       close(f);
       return -1;
   }
   close(f);       
   if (x < 0) x = ~x;
   return x;
}
+2

( , )

ISAAC (, , , ). 2 ^ 8295.

, .

+1

, . , . , Yarrow , , .

OpenSSL API, . Mozilla API, , .

, , Boost.Random, .

0

.

, .

For example, using the Microsoft GUID generator will give you a random identifier that is less likely to be repeated and takes into account things like time and computer.

-2
source

Time is usually the most random operation, which is also cheap to execute, but it can still be predicted.

If you need true randomness, using some kind of external input is your only solution.

Quantum Random Bit Generator is one service that provides such data.

-4
source

All Articles