forked from jefetienne/daggerfall-unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFRandom.cs
More file actions
100 lines (90 loc) · 2.98 KB
/
DFRandom.cs
File metadata and controls
100 lines (90 loc) · 2.98 KB
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
98
99
100
// Project: Daggerfall Tools For Unity
// Copyright: Copyright (C) 2009-2020 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Gavin Clayton (interkarma@dfworkshop.net)
// Contributors:
//
// Notes:
//
namespace DaggerfallWorkshop
{
/// <summary>
/// Reimplementing key parts of Daggerfall's random library.
/// This ensures critical random number sequences (e.g. building names)
/// will match Daggerfall's output across all platforms.
/// </summary>
public static class DFRandom
{
static ulong next = 1;
static ulong savedNext;
public static uint Seed
{
get { return (uint)next; }
set { next = value; }
}
/// <summary>
/// Seed random generator.
/// </summary>
/// <param name="seed">Seed int.</param>
public static void srand(int seed)
{
next = (uint)seed;
}
/// <summary>
/// Seed random generator.
/// </summary>
/// <param name="seed">Seed uint.</param>
public static void srand(uint seed)
{
next = seed;
}
/// <summary>
/// Generate a random number.
/// </summary>
/// <returns></returns>
public static uint rand()
{
next = next * 1103515245 + 12345;
return ((uint)((next >> 16) & 0x7FFF));
}
/// <summary>
/// Generates a random number between min and max (exclusive).
/// </summary>
/// <param name="min">Minimum number.</param>
/// <param name="max">Maximum number (exclusive).</param>
/// <returns>Random number between min and max - 1.</returns>
public static int random_range(int min, int max)
{
return (int)rand() % (max - min) + min;
}
/// <summary>
/// Generates a random number between min and max (inclusive).
/// </summary>
/// <param name="min">Minimum number.</param>
/// <param name="max">Maximum number (inclusive).</param>
/// <returns>Random number between min and max - 1.</returns>
public static int random_range_inclusive(int min, int max)
{
return (int)rand() % (max - min + 1) + min;
}
/// <summary>
/// Generates a random number between 0 and max (exclusive).
/// </summary>
/// <param name="max">Maximum number (exclusive).</param>
/// <returns>Random number between 0 and max - 1.</returns>
public static int random_range(int max)
{
return (int)rand() % max;
}
public static void SaveSeed()
{
savedNext = next;
}
public static void RestoreSeed()
{
next = savedNext;
}
}
}