Skip to content

Instantly share code, notes, and snippets.

@thebeardphantom
Created December 16, 2020 10:27
Show Gist options
  • Save thebeardphantom/f9ebca562b293bc3d296c0a6a0f8a5a1 to your computer and use it in GitHub Desktop.
Save thebeardphantom/f9ebca562b293bc3d296c0a6a0f8a5a1 to your computer and use it in GitHub Desktop.
A mechanism to use FastNoiseLite as a proper RNG class.
using System.Collections.Generic;
using UnityEngine;
public class FastNoiseRNG : FastNoiseLite
{
#region Fields
private ulong _position;
#endregion
#region Methods
public float Next()
{
var noise = GetNoise(_position++, 0);
// Remap from (-1, 1) to (0, 1)
noise = (noise + 1f) / 2f;
return noise;
}
public int NextInt(int max)
{
return NextInt(0, max);
}
public int NextInt(int min, int max)
{
var noise = Next();
var value = Mathf.Lerp(min, max, noise);
return Mathf.RoundToInt(value);
}
public T SelectRandom<T>(IReadOnlyList<T> list)
{
var count = list.Count;
return count == 0 ? default : list[NextInt(count - 1)];
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment