summaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/vespa/vespalib/util/rand48.h
blob: 441f8e6e10fce9b7dff585f0743eb5525e329cbf (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once


namespace search {

/*
 * Simple random generator based on lrand48() spec.
 */
class Rand48
{
private:
    uint64_t _state;
public:
    void
    srand48(long seed)
    {
        _state = ((static_cast<uint64_t>(seed & 0xffffffffu)) << 16) + 0x330e;
    }

    Rand48(void)
        : _state(0)
    {
        srand48(0x1234abcd);
    };
    void iterate(void) {
        _state = (UINT64_C(0x5DEECE66D) * _state + 0xb) &
                 UINT64_C(0xFFFFFFFFFFFF);
    }
    /*
     * Return value from 0 to 2^31 - 1
     */
    long lrand48(void) {
        iterate();
        return static_cast<long>(_state >> 17);
    }
};

} // namespace search