68 lines
1.4 KiB
C#
68 lines
1.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using Random = System.Random;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using UnityEngine;
|
|
|
|
namespace OHM.UnityToolkit
|
|
{
|
|
public static class RandomExtensions
|
|
{
|
|
public static RandomState Save(Random random)
|
|
{
|
|
using (MemoryStream stream = new MemoryStream())
|
|
{
|
|
new BinaryFormatter().Serialize(stream, random);
|
|
return new RandomState(stream.ToArray());
|
|
}
|
|
}
|
|
|
|
public static Random Restore(RandomState state)
|
|
{
|
|
using (MemoryStream stream = new MemoryStream(state.State))
|
|
{
|
|
return (Random)new BinaryFormatter().Deserialize(stream);
|
|
}
|
|
}
|
|
|
|
public static bool Bool(Random random)
|
|
{
|
|
return random.NextDouble() >= 0.5;
|
|
}
|
|
|
|
public static int MinMax(Random random, int n1, int n2)
|
|
{
|
|
if (n1 == n2)
|
|
{
|
|
return n1;
|
|
}
|
|
int lo = Mathf.Min(n1, n2);
|
|
int hi = Mathf.Max(n1, n2);
|
|
return lo + random.Next() % (hi - lo + 1);
|
|
}
|
|
|
|
public static int ListIndex(Random random, int listCount)
|
|
{
|
|
return MinMax(random, 0, listCount - 1);
|
|
}
|
|
|
|
public static int ListIndex(Random random, ICollection collection)
|
|
{
|
|
return ListIndex(random, collection.Count);
|
|
}
|
|
|
|
public static T ListElem<T>(Random random, List<T> list)
|
|
{
|
|
return default(T);
|
|
}
|
|
|
|
public static float MinMax(Random random, float min, float max)
|
|
{
|
|
return (float)((double)(max - min) * random.NextDouble() + (double)min);
|
|
}
|
|
|
|
}
|
|
}
|