File: random.cpp

package info (click to toggle)
indigo 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 48,936 kB
  • sloc: ansic: 332,816; cpp: 169,470; python: 20,033; java: 13,701; cs: 9,979; asm: 8,475; sql: 6,743; xml: 6,354; javascript: 1,245; sh: 555; php: 506; makefile: 54
file content (76 lines) | stat: -rw-r--r-- 1,412 bytes parent folder | download
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "math/random.h"
#include "base_c/defs.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

using namespace indigo;

Random::Random()
{
    randSeed = rand();
}

Random::Random(int seed)
{
    randSeed = seed;
}

void Random::setSeed(unsigned long long x)
{
    randSeed = x;
}

unsigned int Random::next()
{
    //   printf("%lld %lld \n", randSeed, 6364136223846793005LL);
    randSeed = 6364136223846793005LL * randSeed + 1;
    //   printf("%lld %lld \n", randSeed, 6364136223846793005LL);
    return (unsigned int)(randSeed >> 16);
}

unsigned int Random::next(int mod)
{
    if (mod > 0)
        return next() % mod;
    if (mod < 0)
        return -(next() % -mod);
    return 0;
}

unsigned int Random::nextBounded(int l, int r)
{
    return __min(l, r) + next(abs(r - l));
}

unsigned int Random::nextLarge(int mod)
{
    int x = next();
    if ((1LL << 32) - x > mod)
        return x % mod;

    int max = (1LL << 32) - (1LL << 32) % mod;
    if (x < max)
        return x % mod;
    return nextLarge(mod);
}

unsigned long long Random::nextLong()
{
    return ((unsigned long long)next() << 32) + next();
}

unsigned long long Random::nextLong(unsigned long long mod)
{
    return nextLong() % mod;
}

double Random::nextDouble()
{
    return 1.0 * next() / (1LL << 32);
}

double Random::nextDoubleBounded(double l, double r)
{
    return __min(l, r) + nextDouble() * fabs(r - l);
}