aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/random/friendfinder.cpp
blob: 0750e506f09099df3aab589a384c0d15d6a3d624 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/util/random.h>
#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/locale/c.h>
#include <cmath>
#include <vector>

int main(int argc, char **argv)
{
    vespalib::RandomGen rnd(1);

    double logmean = std::log(1000.0);
    double lstddev = std::log(2.0);

    if (argc > 2) {
        lstddev = std::log(vespalib::locale::c::strtod(argv[--argc], NULL));
        logmean = std::log(vespalib::locale::c::strtod(argv[--argc], NULL));
    } else if (argc > 1) {
        logmean = std::log(vespalib::locale::c::strtod(argv[--argc], NULL));
    }

    fprintf(stderr, "100 typical friendlist sizes: ");
    for (int i = 0; i < 100; ++i) {
        int32_t want = (uint32_t)std::exp(rnd.nextNormal(logmean, lstddev));
        fprintf(stderr, " %u", want);
    }
    fprintf(stderr, "\n");

    uint32_t person = 0;
    while (!feof(stdin)) {
        ++person;
        std::vector<vespalib::string> friends;
        int32_t want = (uint32_t)std::exp(rnd.nextNormal(logmean, lstddev));
        if (want < 17) want = (uint32_t)(std::exp(logmean)+0.99);
        if (want < 1) want = 1;

        printf("me: %u friends:", person);
        while (want > 0) {
            char line[100];
            if (fgets(line, 100, stdin) == NULL) {
                break;
            }
            if (rnd.nextUint32() % 42 == 17) {
                vespalib::string s(line);
                s.chomp();
                friends.push_back(s);
                --want;
            }
        }
        while (!friends.empty()) {
            printf(" %s", friends.back().c_str());
            friends.pop_back();
        }
        printf("\n");
        fflush(stdout);
    }
}