Strange behavior with rand ()

Why is the condition rand ()% 3 true approximately? EVERY 3 times? rand realy random isn't it?

-2
source share
3 answers

You understand, of course, that even an honest coin can give you ten goals in a row. There is a probability that can be assigned to this combination.

A fair coin will give half goals and half a tail in many trials, but it does not guarantee 50/50 for a shorter stroke.

Your own experience of the physical world tells you that your conclusion is wrong. The same is true for rand ().

+3
source

As the Linux man page forrand() says:

, rand() , . , , .

, ( ).

rand() , , ( ). , :

  • MSVC 2010 rand():

     return( ((ptd->_holdrand = ptd->_holdrand * 214013L
                + 2531011L) >> 16) & 0x7fff );
    
  • rand() IAR:

     (x) * 1664525L + 1013904223L           
    

, MSVC 2010 rand() , , , man- Linux .

, , , - Mersenne twister .

+1

:

#include<iostream.h>
#include<stdlib.h>
int main()
{
    srand(NULL);       
    cout << rand() % 10 + 1;
}

10 , 1 . :

#include<iostream.h>
#include<stdlib.h>
//new include:
#include<time.h>
int main()
{
    srand(time(NULL));       
    cout << rand() % 10 + 1;
}

, RANDOMIZER (srand()), , , 1. , ". , .

0

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


All Articles