Skip to content

Instantly share code, notes, and snippets.

@mattatz
Created January 11, 2017 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattatz/33a2372248125cf116c7c104690585f8 to your computer and use it in GitHub Desktop.
Save mattatz/33a2372248125cf116c7c104690585f8 to your computer and use it in GitHub Desktop.
Random class for Unity.
using Random = System.Random;
using UnityEngine;
namespace mattatz {
public class Rand {
Random rnd;
public Rand(int seed) {
rnd = new Random(seed);
}
public float Sample01() {
return (float)rnd.NextDouble();
}
public int SampleRange(int a, int b) {
var t = Sample01();
return Mathf.FloorToInt(Mathf.Lerp(a, b, t));
}
public float SampleRange(float a, float b) {
var t = Sample01();
return Mathf.Lerp(a, b, t);
}
public Vector2 SampleUnitCircle () {
var x = (Sample01() - 0.5f) * 2f;
var y = (Sample01() - 0.5f) * 2f;
return new Vector2(x, y);
}
public Vector3 SampleUnitSphere () {
var x = (Sample01() - 0.5f) * 2f;
var y = (Sample01() - 0.5f) * 2f;
var z = (Sample01() - 0.5f) * 2f;
return new Vector3(x, y, z);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment