초기커밋
This commit is contained in:
169
ServerCore/Random/RandomHelper.cs
Normal file
169
ServerCore/Random/RandomHelper.cs
Normal file
@@ -0,0 +1,169 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
|
||||
|
||||
namespace ServerCore;
|
||||
|
||||
public static class RandomHelper
|
||||
{
|
||||
// 만분율을 기본 비율단위로 사용 한다 !!!
|
||||
public static readonly Int32 DefaultRatio = 10000;
|
||||
private static Random random = new Random();
|
||||
|
||||
public static double nextDouble(double min, double max)
|
||||
{
|
||||
return random.NextDouble() * (max - min) + min;
|
||||
}
|
||||
|
||||
public static double rangeDouble(double min, double max)
|
||||
{
|
||||
return random.NextDouble() * (max - min) + min;
|
||||
}
|
||||
|
||||
public static string randomString(Int32 length)
|
||||
{
|
||||
const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
|
||||
}
|
||||
|
||||
public static string randomName(Int32 length)
|
||||
{
|
||||
string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "sh", "zh", "t", "v", "w", "x", "z" };
|
||||
string[] vowels = { "a", "e", "i", "o", "u", "ae", "y" };
|
||||
StringBuilder Name = new StringBuilder();
|
||||
Name.Append(consonants[random.Next(consonants.Length)].ToUpper());
|
||||
Name.Append(vowels[random.Next(vowels.Length)]);
|
||||
Int32 idx = 2;
|
||||
while (idx < length)
|
||||
{
|
||||
Name.Append(consonants[random.Next(consonants.Length)]);
|
||||
Name.Append(vowels[random.Next(vowels.Length)]);
|
||||
idx += 2;
|
||||
if (length - idx == 1)
|
||||
{
|
||||
Name.Append(consonants[random.Next(consonants.Length)]);
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
return Name.ToString();
|
||||
}
|
||||
|
||||
public static Int32 number(Int32 max)
|
||||
{
|
||||
Int32 val = random.Next(0, max);
|
||||
return val;
|
||||
}
|
||||
|
||||
public static Int32 range(Int32 min, Int32 max)
|
||||
{
|
||||
Int32 val = random.Next(min, max);
|
||||
return val;
|
||||
}
|
||||
// 만분율 기반 확률 계산
|
||||
public static bool defaultProb(Int32 target_prob)
|
||||
{
|
||||
Int32 prob = random.Next(1, (Int32)DefaultRatio + 1);
|
||||
|
||||
return prob < target_prob;
|
||||
}
|
||||
// 만분율 기반 확률 계산
|
||||
public static bool defaultProb(Int64 target_prob)
|
||||
{
|
||||
return defaultProb((Int32)target_prob);
|
||||
}
|
||||
// min 과 max를 포함한 사잇값
|
||||
public static Int32 between(Int32 min, Int32 max)
|
||||
{
|
||||
return random.Next(min, max + 1);
|
||||
}
|
||||
|
||||
public static Int32 between(Int64 min, Int64 max)
|
||||
{
|
||||
return random.Next((Int32)min, (Int32)max + 1);
|
||||
}
|
||||
|
||||
public static Int32 next()
|
||||
{
|
||||
return random.Next();
|
||||
}
|
||||
|
||||
public static Int32 next(Int32 min, Int32 max)
|
||||
{
|
||||
return random.Next(min, max);
|
||||
}
|
||||
|
||||
public static float next(float min, float max)
|
||||
{
|
||||
Int32 min_int = (Int32)(min * DefaultRatio);
|
||||
Int32 max_int = (Int32)(max * DefaultRatio);
|
||||
|
||||
return (float)(between(min_int, max_int) / DefaultRatio);
|
||||
}
|
||||
|
||||
public static Int32 next(Int32 max)
|
||||
{
|
||||
return random.Next(max);
|
||||
}
|
||||
|
||||
public static void shuffleList<T>(IList<T> list)
|
||||
{
|
||||
Int32 count = list.Count;
|
||||
while (count > 1)
|
||||
{
|
||||
Int32 key = random.Next(count);
|
||||
T value = list[key];
|
||||
list[key] = list[count - 1];
|
||||
list[count - 1] = value;
|
||||
|
||||
count--;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Int32> randomIndex(Int32 count)
|
||||
{
|
||||
List<Int32> index_list = new List<Int32>();
|
||||
for (Int32 i = 0; i < count; ++i)
|
||||
{
|
||||
index_list.Add(i);
|
||||
}
|
||||
|
||||
shuffleList<Int32>(index_list);
|
||||
return index_list;
|
||||
}
|
||||
|
||||
public static Vector3 randomPointInCicle(Vector3 centerPos, float radius)
|
||||
{
|
||||
var angle = random.NextDouble() * Math.PI * 2;
|
||||
var rad = random.NextDouble() * radius;
|
||||
|
||||
float x = (float)(centerPos.X + rad * Math.Cos(angle));
|
||||
float z = (float)(centerPos.Z + rad * Math.Sin(angle));
|
||||
|
||||
return new Vector3(x, centerPos.Y, z);
|
||||
}
|
||||
|
||||
public static Vector3 randomPointInFanShape(Vector3 centerPos, float radius, float degree)
|
||||
{
|
||||
var angle = degree * MathHelper.Deg2Rad;
|
||||
var rad = random.NextDouble() * radius;
|
||||
|
||||
float x = (float)(centerPos.X + rad * Math.Cos(angle));
|
||||
float z = (float)(centerPos.Z + rad * Math.Sin(angle));
|
||||
|
||||
return new Vector3(x, centerPos.Y, z);
|
||||
}
|
||||
|
||||
public static Vector3 randomPointInRectangle(Vector3 centerPos, float xScale, float zScale)
|
||||
{
|
||||
var x = centerPos.X + (float)rangeDouble(centerPos.X - (xScale * 0.5), xScale + (xScale * 0.5));
|
||||
var z = centerPos.Z + (float)rangeDouble(centerPos.Z - (zScale * 0.5), zScale + (zScale * 0.5));
|
||||
|
||||
return new Vector3(x, centerPos.Y, z);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user