Just what!? Standart rand() in C++ or undeniable proof of the existence of magic!
Difference between en2 and en3, changed 2,257 character(s)
Until recently, I knew that **rand()** in C++ works awfully with _random_shuffle_ and it was not credible, but continued to use it for most tasks.. But recently I wrote some code and I cannot explain the result. Maybe you can?↵

**I advise you to read to the end, it is really very interesting!**↵

At each step, this code generates a random value from **0** to **1**. As soon as **11** (for example) identical values equal **1** were received in a row, index number of current value is added to the vector. This operation is repeated a number of times and each time it starts from the beginning.↵

Let's output vector values and look at the last 15 of them for convenience.↵

Code here:↵

<spoiler summary="CODE">↵
~~~~~↵
#include <bits/stdc++.h>↵
using namespace std;↵

int main() {↵
    srand(time(0));↵
    vector<int> v;↵
    for(int i = 0; i < 100; ++ i) {↵
        int it = 0, tec = 0;↵
        while(true) {↵
            ++ it;↵
            if(tec == 11) {↵
                v.push_back(it);↵
                break;↵
            }↵
            if(rand() % 2) ++ tec;↵
            else tec = 0;↵
        }↵
    }↵
    for(int i = v.size() - 15; i < v.size(); ++ i)↵
        cout << v[i] << endl;↵
}↵
~~~~~↵
</spoiler>↵

<spoiler summary="OUTPUT1">↵
6906↵
4109↵
125↵
9564↵
245↵
7841↵
10130↵
3685↵
265↵
272↵
9041↵
6916↵
4918↵
9028↵
13739↵
</spoiler>↵

<spoiler summary="OUTPUT2">↵
3685↵
265↵
272↵
9041↵
6916↵
4918↵
9028↵
13739↵
1796↵
133↵
7302↵
645↵
201↵
1207↵
192↵
</spoiler>↵

<spoiler summary="OUTPUT3">↵
16608↵
6906↵
4109↵
125↵
9564↵
245↵
7841↵
10130↵
3685↵
265↵
272↵
9041↵
6916↵
4918↵
9028↵
</spoiler>↵

<spoiler summary="OUTPUT4">↵
245↵
7841↵
10130↵
3685↵
265↵
272↵
9041↵
6916↵
4918↵
9028↵
13739↵
1796↵
133↵
7302↵
645↵
</spoiler>↵

At first glance it seems that the values are random. But let's try to sort the vector.↵

Code here:↵

<spoiler summary="CODE">↵
~~~~~↵
#include <bits/stdc++.h>↵
using namespace std;↵

int main() {↵
    srand(time(0));↵
    vector<int> v;↵
    for(int i = 0; i < 100; ++ i) {↵
        int it = 0, tec = 0;↵
        while(true) {↵
            ++ it;↵
            if(tec == 11) {↵
                v.push_back(it);↵
                break;↵
            }↵
            if(rand() % 2) ++ tec;↵
            else tec = 0;↵
        }↵
    }↵
    sort(v.begin(), v.end());↵
    for(int i = v.size() - 15; i < v.size(); ++ i)↵
        cout << v[i] << endl;↵
}↵
~~~~~↵
</spoiler>↵

<spoiler summary="OUTPUT1">↵
9041↵
9564↵
9564↵
9564↵
10130↵
10130↵
10130↵
10130↵
13739↵
13739↵
13739↵
13739↵
16608↵
16608↵
16608↵
</spoiler>↵

<spoiler summary="OUTPUT2">↵
9041↵
9041↵
9564↵
9564↵
9564↵
10130↵
10130↵
10130↵
13739↵
13739↵
13739↵
13739↵
16608↵
16608↵
16608↵
</spoiler>↵

<spoiler summary="OUTPUT3">↵
9564↵
9564↵
9564↵
9564↵
10130↵
10130↵
10130↵
10130↵
13739↵
13739↵
13739↵
16608↵
16608↵
16608↵
16608↵
</spoiler>↵

<spoiler summary="OUTPUT4">↵
9564↵
9564↵
9564↵
9564↵
10130↵
10130↵
10130↵
10130↵
13739↵
13739↵
13739↵
13739↵
16608↵
16608↵
16608↵
</spoiler>↵

It should be noted that the output is different each time, but the individual values are the same. Moreover, these individual values are repeated three to four times.↵

- Using of _mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());_ instead of _rand()_ gives a random output array.↵

- If we add a line that will not affect the answer in any way, but will affect the time difference between the rand() calls we also get random values in the output array. In doing so, we have to use random delay. If the delay is fixed, we will not get random values.↵

<spoiler summary="CODE1, rnd(), normal random values">↵
~~~~~↵
#include <bits/stdc++.h>↵
using namespace std;↵

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());↵

int main() {↵
    srand(time(0));↵
    vector<int> v;↵
    for(int i = 0; i < 100; ++ i) {↵
        int it = 0, tec = 0;↵
        while(true) {↵
            ++ it;↵
            if(tec == 11) {↵
                v.push_back(it);↵
                break;↵
            }↵
            if(rnd() % 2) ++ tec;↵
            else tec = 0;↵
        }↵
    }↵
    sort(v.begin(), v.end());↵
    for(int i = v.size() - 15; i < v.size(); ++ i)↵
        cout << v[i] << endl;↵
}↵
~~~~~↵
</spoiler>↵

<spoiler summary="CODE2, random delay between calls rand(), normal random values">↵
~~~~~↵
#include <bits/stdc++.h>↵
using namespace std;↵

int main() {↵
    srand(time(0));↵
    vector<int> v;↵
    for(int i = 0; i < 100; ++ i) {↵
        int it = 0, tec = 0;↵
        while(true) {↵
            //↵
            int u = 1;↵
            for(int j = 0; j < rand() % 10; ++ j) u *= 3;↵
            //↵
            ++ it;↵
            if(tec == 11) {↵
                v.push_back(it);↵
                break;↵
            }↵
            if(rand() % 2) ++ tec;↵
            else tec = 0;↵
        }↵
    }↵
    sort(v.begin(), v.end());↵
    for(int i = v.size() - 15; i < v.size(); ++ i)↵
        cout << v[i] << endl;↵
}↵
~~~~~↵
</spoiler>↵

With a fixed delay, we also get magic values, even if the delay is large.↵

<spoiler summary="CODE3, fixed delay between calls rand(), magic values">↵
~~~~~↵
#include <bits/stdc++.h>↵
using namespace std;↵

int main() {↵
    srand(time(0));↵
    vector<int> v;↵
    for(int i = 0; i < 100; ++ i) {↵
        int it = 0, tec = 0;↵
        while(true) {↵
            //↵
            int u = 1;↵
            for(int j = 0; j < 10; ++ j) u *= 3;↵
            //↵
            ++ it;↵
            if(tec == 11) {↵
                v.push_back(it);↵
                break;↵
            }↵
            if(rand() % 2) ++ tec;↵
            else tec = 0;↵
        }↵
    }↵
    sort(v.begin(), v.end());↵
    for(int i = v.size() - 15; i < v.size(); ++ i)↵
        cout << v[i] << endl;↵
}↵
~~~~~↵
</spoiler>↵

Now let's instead of an empty loop in the third code call _rand()_.↵

<spoiler summary="CODE">↵
~~~~~↵
#include <bits/stdc++.h>↵
using namespace std;↵

int main() {↵
    srand(time(0));↵
    vector<int> v;↵
    for(int i = 0; i < 100; ++ i) {↵
        int it = 0, tec = 0;↵
        while(true) {↵
            //↵
            rand();↵
            //↵
            ++ it;↵
            if(tec == 11) {↵
                v.push_back(it);↵
                break;↵
            }↵
            if(rand() % 2) ++ tec;↵
            else tec = 0;↵
        }↵
    }↵
    sort(v.begin(), v.end());↵
    for(int i = v.size() - 25; i < v.size(); ++ i)↵
        cout << v[i] << endl;↵
}↵
~~~~~↵
</spoiler>↵


<spoiler summary="OUTPUT1">↵
5447↵
5447↵
5447↵
5447↵
5447↵
5447↵
9664↵
9664↵
9664↵
9664↵
9664↵
9664↵
9664↵
9752↵
9752↵
9752↵
9752↵
9752↵
9752↵
13473↵
13473↵
13473↵
13473↵
13473↵
13473↵
</spoiler>↵


<spoiler summary="OUTPUT2">↵
5077↵
5447↵
5447↵
5447↵
5447↵
5447↵
5447↵
9664↵
9664↵
9664↵
9664↵
9664↵
9664↵
9752↵
9752↵
9752↵
9752↵
9752↵
9752↵
13473↵
13473↵
13473↵
13473↵
13473↵
13473↵
</spoiler>↵


<spoiler summary="OUTPUT3">↵
5447↵
5447↵
5447↵
5447↵
5447↵
5447↵
9664↵
9664↵
9664↵
9664↵
9664↵
9664↵
9752↵
9752↵
9752↵
9752↵
9752↵
9752↵
9752↵
13473↵
13473↵
13473↵
13473↵
13473↵
13473↵
</spoiler>↵

Values become repeated much more often..↵

<spoiler summary="CODE">↵

~~~~~↵
#include <bits/stdc++.h>↵
using namespace std;↵
int main() {↵
    srand(time(0));↵
    vector<int> v;↵
    for(int i = 0; i < 100; ++ i) {↵
        int it = 0, tec = 0;↵
        while(true) {↵
            //↵
            for(int j = 0; j < 2; ++ j) rand();↵
            //↵
            ++ it;↵
            if(tec == 11) {↵
                v.push_back(it);↵
                break;↵
            }↵
            if(rand() % 2) ++ tec;↵
            else tec = 0;↵
        }↵
    }↵
    sort(v.begin(), v.end());↵
    for(int i = v.size() - 25; i < v.size(); ++ i)↵
        cout << v[i] << endl;↵
}↵
~~~~~↵
</spoiler>↵


<spoiler summary="OUTPUT">↵
9213↵
9213↵
9213↵
9213↵
9213↵
9213↵
9213↵
9213↵
9213↵
9213↵
9213↵
15676↵
15676↵
15676↵
15676↵
15676↵
15676↵
15676↵
15676↵
15676↵
15676↵
15676↵
15676↵
15676↵
15676↵
</spoiler>↵

If we call the method twice, the values are repeated more often.↵

I believe that this is not quite a trivial algorithm and the result is rather strange. If you have any idea how to explain this, please state your thoughts in the comments. Thanks for reading!

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English seo 2019-02-15 14:29:11 2257
en2 English seo 2019-02-15 01:00:46 2 (published)
en1 English seo 2019-02-15 01:00:06 6059 Initial revision (saved to drafts)