Skip to content

Commit

Permalink
First Initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
miputra committed Nov 17, 2021
1 parent ced48bd commit d58e530
Show file tree
Hide file tree
Showing 17 changed files with 1,037 additions and 0 deletions.
31 changes: 31 additions & 0 deletions RandEXom.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31205.134
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RandomisatorOutputTest", "RandomisatorOutputTest\RandomisatorOutputTest.csproj", "{64F54633-6EB9-4CFB-B289-FBE5B6C277EB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RandEXom", "RandEXom\RandEXom.csproj", "{ACD28E04-F56C-4608-A4C4-E5DCE4996C12}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{64F54633-6EB9-4CFB-B289-FBE5B6C277EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{64F54633-6EB9-4CFB-B289-FBE5B6C277EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{64F54633-6EB9-4CFB-B289-FBE5B6C277EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{64F54633-6EB9-4CFB-B289-FBE5B6C277EB}.Release|Any CPU.Build.0 = Release|Any CPU
{ACD28E04-F56C-4608-A4C4-E5DCE4996C12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ACD28E04-F56C-4608-A4C4-E5DCE4996C12}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ACD28E04-F56C-4608-A4C4-E5DCE4996C12}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ACD28E04-F56C-4608-A4C4-E5DCE4996C12}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A0F68DB5-5DE9-49C5-B30D-5C99EE5ADB81}
EndGlobalSection
EndGlobal
91 changes: 91 additions & 0 deletions RandEXom/Framework/Gacha.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RandEXom.RandomLib;
using RandEXom.SeedLib;

namespace RandEXom.Framework
{
/// <summary>
/// Gacha or random picker framework. Heavy on memory because saving the entire item but low in process
/// </summary>
/// <typeparam name="T"></typeparam>
public class Gacha<T> : RandEXom.Interface.IGacha<T>
{
List<T> items_init = new List<T>();
List<T> items_current = new List<T>();

RandEXom.Interface.IRandom rand;

public Gacha()
{
rand = new RandomLib.NetRandom(new Seed());
}
public Gacha(long? seed)
{
rand = new RandomLib.NetRandom(new Seed(seed));
}

public Gacha(RandEXom.Interface.IRandom framework)
{
rand = framework;
}

public virtual void AddItem(T item, int count)
{
for(int i=0; i < count; i++)
{
items_init.Add(item);
items_current.Add(item);
}
}

public virtual void RemoveEmpty(T item)
{
items_init.Clear();
items_init.AddRange(items_current);
}

public virtual void Refill()
{
items_current.Clear();
items_current.AddRange(items_init);
}

public virtual void Remove(T value)
{
items_init.Remove(value);
items_current.Remove(value);
}

public virtual T Pull()
{
if (items_current.Count <= 0)
return default(T);
int n = rand.NextInt(0, items_current.Count);
T get = items_current[n];
items_current.RemoveAt(n);
return get;
}

/// <inheritdoc/>
public int Count()
{
return items_current.Count();
}
/// <inheritdoc/>
public int Count(T type)
{
dynamic t1 = type;
return items_current.FindAll(x => x == t1).Count();
}
/// <inheritdoc/>
public int CountType()
{
HashSet<T> set = new HashSet<T>(items_current);
return set.Count;
}
}
}
191 changes: 191 additions & 0 deletions RandEXom/Framework/GachaBatched.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RandEXom.RandomLib;
using RandEXom.SeedLib;

namespace RandEXom.Framework
{
/// <summary>
/// Gacha or random picker framework. Heavy on process, but low on memory because only using sample
/// </summary>
/// <typeparam name="T"></typeparam>
public class GachaBatched<T> : RandEXom.Interface.IGacha<T>
{
Dictionary<T, int> items_init = new Dictionary<T, int>();
Dictionary<T, int> items_current = new Dictionary<T, int>();

RandEXom.Interface.IRandom rand;

/// <inheritdoc/>
public int Count()
{
int c = 0;
foreach(KeyValuePair<T,int> p in items_current)
{
c += p.Value;
}
return c;
}

/// <inheritdoc/>
public int Count(T type)
{
if (items_current.ContainsKey(type))
return items_current[type];
return 0;
}

/// <inheritdoc/>
public int CountType()
{
return items_current.Count;
}

public GachaBatched(long seed)
{
rand = new RandomLib.NetRandom(new Seed(seed));
}

public GachaBatched()
{
rand = new RandomLib.NetRandom(new Seed());
}

public GachaBatched(RandEXom.Interface.IRandom framework)
{
rand = framework;
}

public void AddItem(T item, int count)
{
//if(items_init.Find(x=> x.value == item))
//items_init.Add(new Item<T>(item,count));
if (!items_init.ContainsKey(item))
{
items_init.Add(item, 0);
items_current.Add(item, 0);
}
items_init[item] += count;
items_current[item] += count;
}

/// <inheritdoc/>
public void RemoveEmpty(T item, bool isIterative = false)
{
switch (isIterative)
{
case false: //Make a new directory instead. Heavy in memory size, and will create a dump
items_init = new Dictionary<T, int>(items_current);
break;
case true: //Iterative. Heavy on process, but will not create a dump
items_init.Clear();
foreach (KeyValuePair<T, int> pair in items_current)
{
items_init.Add(pair.Key, pair.Value);
}
break;
}
}
/// <inheritdoc/>
public void Refill(bool isIterative = false)
{
switch (isIterative)
{
case false: //Make a new directory instead. Heavy in memory size, and will create a dump
items_current = new Dictionary<T, int>(items_init);
break;
case true: //Iterative. Heavy on process, but will not create a dump
items_current.Clear();
foreach (KeyValuePair<T, int> pair in items_init)
{
items_current.Add(pair.Key, pair.Value);
}
break;
}
}

public void Remove(T value)
{
items_init.Remove(value);
items_current.Remove(value);
}

public T Pull()
{
if (items_current.Keys.Count <= 0)
return default(T);

int n = Count();
int r = rand.NextInt(0, n);
Dictionary<T, double> temp = new Dictionary<T, double>();

double minVal = 0;
foreach (KeyValuePair<T,int> pair in items_current)
{
double p = (double)pair.Value / (double)n;
if (temp.Count <= 0)
minVal = p;
else
{
minVal = Math.Min(p, minVal);
}
temp.Add(pair.Key, p);
}
double maxVal = 0;
while (minVal < 2)
{
maxVal = 0;
for(int i=0; i < temp.Keys.Count; i++)
{
T key = temp.Keys.ToList()[i];
temp[key] *= 10;
if (i == 0) {
minVal = temp[key];
} else
{
minVal = Math.Min(temp[key], minVal);
}
maxVal = Math.Max(maxVal, temp[key]);
}
}

List<T> o = temp.Keys.OrderBy(x => temp[x]).ToList();
foreach (T get in o)
{

if (r % Math.Round(maxVal - temp[get]) == 0)
{
items_current[get] -= 1;
if (items_current[get] <= 0)
items_current.Remove(get);
return get;
}
}

if(o.Count > 0)
{
T get = o[o.Count - 1];
items_current[get] -= 1;
if (items_current[get] <= 0)
items_current.Remove(get);
return get;
}

return default(T);

}

public void RemoveEmpty(T item)
{
RemoveEmpty(item,false);
}

public void Refill()
{
Refill(false);
}
}
}
43 changes: 43 additions & 0 deletions RandEXom/Interface/IGacha.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RandEXom.Interface
{
public interface IGacha<T>
{
/// <summary>
/// Count every item in pool
/// </summary>
/// <returns></returns>
int Count();
/// <summary>
/// Count for specific item in pool
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
int Count(T type);
/// <summary>
/// Counting how many type in pool
/// </summary>
/// <returns></returns>
int CountType();
void AddItem(T item, int count);
/// <summary>
/// make the current item pool become init pool
/// </summary>
/// <param name="item"></param>
/// <param name="isIterative"></param>
void RemoveEmpty(T item);

/// <summary>
/// set current pool to init pool
/// </summary>
/// <param name="isIterative"></param>
void Refill();
void Remove(T value);
T Pull();
}
}
17 changes: 17 additions & 0 deletions RandEXom/Interface/IRandom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RandEXom.Interface
{
public interface IRandom
{
string GetSeed();
int NextInt(int min, int max);
void NextBytes(byte[] buffers);
long NextLong(long min, long max);

}
}
17 changes: 17 additions & 0 deletions RandEXom/Interface/ISeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RandEXom.Interface
{

public interface ISeed
{
long init { get;}
long now { get;}
long previous { get; }
void Next();
}
}
Loading

0 comments on commit d58e530

Please sign in to comment.