aboutsummaryrefslogtreecommitdiffstats
path: root/code/editor/editor_random.h
blob: 26c234e0c90771bb06873fe820065f54cdb386cf (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* date = December 21st 2025 1:11 pm */

#ifndef EDITOR_RANDOM_H
#define EDITOR_RANDOM_H

//~ Random 
#include <math.h>

#define PCG_MULTIPLIER_64 6364136223846793005ULL
#define PCG_INCREMENT_64  1442695040888963407ULL

typedef struct random_series random_series;
struct random_series
{
    u64 State;
};

internal void
RandomStep(random_series *Series)
{
    u64 NewState = (Series->State) * PCG_MULTIPLIER_64 + PCG_INCREMENT_64;
    Series->State = NewState;
}

internal void
RandomSeed(random_series *Series, u64 Seed)
{
    Series->State = 0;
    RandomStep(Series);
    Series->State += Seed;
    RandomStep(Series);
}

internal void
RandomLeap(random_series *Series, u64 Delta)
{
    u64 Increment = PCG_INCREMENT_64;
    u64 Multiplier = PCG_MULTIPLIER_64; 
    u64 AccumulatedMult = 1u;
    u64 AccumulatedInc = 0u;
    while(Delta > 0)
    {
        if(Delta & 1)
        {
            AccumulatedMult *= Multiplier;
            AccumulatedInc = AccumulatedInc * Multiplier + Increment;
        }
        Increment = (Multiplier + 1) * Increment;
        Multiplier *= Multiplier;
        Delta /= 2;
    }
    
    u64 NewState = AccumulatedMult * Series->State + AccumulatedInc;
    Series->State = NewState;
}

internal u32
RandomNext(random_series *Series)
{
    u32 Result = 0;
    
    Result = (u32)((Series->State ^ (Series->State >> 22)) >> (22 + (Series->State >> 61)));
    RandomStep(Series);
    
    return Result;
}

internal f32 
RandomF32(random_series *Series)
{
    f32 Result = ldexpf((f32)RandomNext(Series), -32);
    return Result;
}

internal f32
RandomUnilateral(random_series *Series)
{
    f32 Result = RandomF32(Series);
    return Result;
}

internal f32
RandomBilateral(random_series *Series)
{
    f32 Result = 2.0f*RandomUnilateral(Series) - 1.0f;
    return Result;
}

internal f32
RandomBetween(random_series *Series, f32 Min, f32 Max)
{
    f32 Range = Max - Min;
    f32 Result = Min + RandomUnilateral(Series)*Range;
    return Result;
}

#endif //EDITOR_RANDOM_H