Project

General

Profile

rand-test.cpp

xyz dragon, February 27, 2017 11:27

 
1
/*
2
g++ -Wall -Wextra -std=c++11 rand-test.cpp && ./a.out
3
*/
4

    
5
#include <cstdlib>
6
#include <iostream>
7
#include <iomanip>
8
#include <vector>
9
#include <algorithm>
10
#include <utility>
11

    
12

    
13
long unsigned int randCopy( void )
14
{
15
    static long unsigned int seed = 1;
16
    seed =  ( seed * 1103515245 + 12345 ) & 0x7fffffff; // 2147483647 = ( ( (long unsigned int)(1) << 31 ) - 1
17
    return seed;
18
}
19

    
20
int main( int /* argc */, char ** /* argv */ )
21
{
22
    srand(1);
23
    // 1804289383   // srand 0
24
    // 1804289383   // srand 1
25
    // 1505335290   // srand 2
26
    auto const nSongsInTracklist     = 300;
27
    auto const nShuffledTracksToPlay = 10*nSongsInTracklist;
28

    
29
    std::vector< int > played( nSongsInTracklist, 0 );
30
    auto nDifferentPlayed = 0;
31
    auto nPlayed = 0;
32

    
33
    for ( ;nPlayed < nShuffledTracksToPlay; ++nPlayed )
34
    {
35
        auto const r = rand();
36
        auto const iToPlay = r % nSongsInTracklist;
37
        nDifferentPlayed += played[ iToPlay ]++ == 0;
38
        if ( nDifferentPlayed == nSongsInTracklist )
39
            break;
40
        //std::cout << std::setw(12) << r << " " << randCopy() << " " << r % n << std::endl;
41
    }
42
    std::vector< std::pair< size_t, int > > playedTuple( played.size(), std::make_pair( size_t(0), 0 ) );
43
    for ( size_t i = 0u; i < playedTuple.size(); ++i )
44
    {
45
        playedTuple[i].first  = i;
46
        playedTuple[i].second = played[i];
47
    }
48
    auto comparator = [](
49
        std::pair< size_t, int > const & a,
50
        std::pair< size_t, int > const & b
51
    )
52
    {
53
        if ( a.second > b.second )
54
            return true;
55
        return false;
56
    };
57
    std::sort( playedTuple.begin(), playedTuple.end(), comparator );
58

    
59
    if ( nDifferentPlayed == nSongsInTracklist )
60
        std::cout << "All songs were played at least one time after " << nPlayed << " tracks being played." << std::endl;
61
    else
62
    {
63
        std::cout << "The following songs weren't played at all after " << nPlayed << " tracks being played" << std::endl;
64
    }
65
    std::cout << "These are the top ten most often played tracks out of " << nSongsInTracklist << " total tracks to play after " << nPlayed << " tracks being played, i.e. at average each track should have been played " << nPlayed/float(nSongsInTracklist) << " times!" << std::endl;
66
    for ( size_t i = 0u; i < std::min( (size_t) 10u, playedTuple.size() ); ++i )
67
    {
68
        std::cout << "  Track " << std::setw(4) << playedTuple[i].first << " played " << std::setw(4) << playedTuple[i].second << " times" << std::endl;
69
    }
70
    std::cout << "These are the 10 least often played songs:" << std::endl;
71
    for ( size_t i = std::max( (long int) 0, (long int)playedTuple.size()-1 - 10 );
72
          i < playedTuple.size(); ++i )
73
    {
74
        std::cout << "  Track " << std::setw(4) << playedTuple[i].first << " played " << std::setw(4) << playedTuple[i].second << " times" << std::endl;
75
    }
76

    
77
    return 0;
78
}