Is rand () predictable in C ++

Possible duplicate:
What is the correct way to use the rand () function in C ++?

When I run the program below, I always get the same values ​​every time. Is rand not a real random function?

int main()
{

 while(1)
 {
 getch();
 cout<<rand()<<endl;
 }

}

In each run, I get the values ​​below.

41

18467

6334

26500

19169

15724

......

+3
source share
5 answers

. rand() - , . "" , rand(), . -, "" (, ), . , , - .

, seed

+11

? , , :)

, , , "" . , , , . tic- . .

+5

Microsoft, rand_s, ( , ): http://msdn.microsoft.com/en-us/library/sxtz2fa8%28VS.80%29.aspx

/dev/urandom Linux CryptGenRandom() Windows, .

+3

srand() .

+2

, "" . , , , - , 2 ^ 48.

, :

long randomLong(unsigned int x) 
{
    x ^= (x << 21); // x is a non zero seed value
    x ^= (x >> 35);
    x ^= (x << 4);
    return x;
}

This is taken from the following article: http://www.jstatsoft.org/v08/i14/paper

What a really interesting article describing some low-cost random number generators

+1
source

Source: https://habr.com/ru/post/1746873/


All Articles