private List <Post> GeneratePosts(ScenarioEnding type) { var selectedPosts = new List <Post>(4); var neutralPosts = 2; var topScenarioCount = 2; var typePosts = posts.Where(post => post.Type == type).ToList(); // Load the two top scenarios var topScenarios = manager.TopScenarios(topScenarioCount); if (topScenarios.Count < topScenarioCount) { neutralPosts += topScenarioCount - topScenarios.Count; } foreach (var scenario in topScenarios) { // Load all posts registered to this scenario var scenarioPostList = typePosts.Where(post => post.scenario == scenario).ToList(); // If there are any, select one random one to return if (scenarioPostList.Count > 0) { selectedPosts.Add(scenarioPostList[UnityEngine.Random.Range(0, scenarioPostList.Count - 1)]); } // If there are none, tell to load one extra post from neutral else { neutralPosts++; } } // Select all neutral posts, i.e. the posts that do not belong to the top 2 scenarios // var neutralPostList = posts.Where(post => !topScenarios.Contains(post.scenario)).ToList(); // FIXME This messes up (might be fixed 2020-03-26) var neutralPostList = typePosts.Where(post => !selectedPosts.Contains(post)).ToList(); // If there are not enough posts to pool from, just return as many as possible if (neutralPosts > neutralPostList.Count) { neutralPosts = neutralPostList.Count; } // If there are no posts left to pool from, simply skip the rest and return if (neutralPosts == 0) { return(selectedPosts); } // Generate random indexes var randIndexes = Random.UniqueIntegers(0, neutralPostList.Count, neutralPosts); // Select posts to return based on the indexes selectedPosts.AddRange(randIndexes.Select(i => neutralPostList[i])); return(selectedPosts); }
public void UniqueIntegersRangePasses() { // Test min > max Assert.Throws <ArgumentException>(() => Random.UniqueIntegers(10, 0, 1)); // Test len < 1 Assert.Throws <ArgumentException>(() => Random.UniqueIntegers(0, 4, 0)); // Test distance Assert.Throws <ArgumentException>(() => Random.UniqueIntegers(0, 4, 5)); // Test tight Assert.Throws <ArgumentException>(() => Random.UniqueIntegers(0, 1, 2)); // Test zeroes Assert.Throws <ArgumentException>(() => Random.UniqueIntegers(0, 0, 0)); // Test valid Assert.DoesNotThrow(() => Random.UniqueIntegers(-8, 50, 53)); }
private static int[] RunSequence(int min, int max, int length) { var sequence = Random.UniqueIntegers(min, max, length); // Check length of array Assert.True(sequence.Length == length); foreach (var val in sequence) { // Check if unique Assert.True(sequence.Count(i => i == val) == 1); // Check if in range Assert.True(val >= min); Assert.True(val < max); } return(sequence); }