Created
October 14, 2023 22:03
-
-
Save jhocking/ec5550128c29a0f9f451f4dceac238cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
// a different way of generating random values that may "feel" better than true randomness | |
// every possible value will show up once, but in a random order | |
// it simply draws from the "bag" every time you get NextValue | |
// | |
// construct either with a list of values, or a size for the most common use | |
// list values like: new {1, 1, 2, 2, 3, 4}; | |
// size constructor makes a list of numbers from 0 to size-1 | |
// incidentally, you may want to call Random.InitState(seed) outside this object | |
// in order to get the same "random" sequence each playthrough | |
public class ShuffleBag { | |
private int currentPosition; | |
private int[] numberList; | |
public int NextValue { | |
get { | |
int val = numberList[currentPosition]; | |
currentPosition++; | |
if (currentPosition >= numberList.Length) { | |
Reset(); | |
} | |
return val; | |
} | |
} | |
public ShuffleBag(int[] numbers) { | |
numberList = ShuffleArray(numbers); | |
} | |
public ShuffleBag(int listSize) { | |
numberList = new int[listSize]; | |
for (int i = 0; i < listSize; ++i) { | |
numberList[i] = i; | |
} | |
numberList = ShuffleArray(numberList); | |
} | |
private void Reset() { | |
int lastValue = numberList[numberList.Length-1]; | |
numberList = ShuffleArray(numberList); | |
// make sure not to repeat last value | |
if (numberList[0] == lastValue) { | |
int r = Random.Range(1, numberList.Length); | |
numberList[0] = numberList[r]; | |
numberList[r] = lastValue; | |
} | |
currentPosition = 0; | |
} | |
// implementation of Knuth shuffle algorithm | |
public static int[] ShuffleArray(int[] numbers) { | |
int[] newArray = numbers.Clone() as int[]; | |
for (int i = 0; i < newArray.Length; ++i) { | |
int tmp = newArray[i]; | |
int r = Random.Range(i, newArray.Length); | |
newArray[i] = newArray[r]; | |
newArray[r] = tmp; | |
} | |
return newArray; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment