aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/rand48.h
blob: 22848cc0713daaf70468ccc9fcb0d7f8ba7c7f95 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <cstdlib>
#include <cstdint>

namespace vespalib {

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

}