static void Main(string[] args) { var random = new Random(); var list = new ListRandom(); var list1 = new ListRandom(); for (int i = 0; i < 20; i++) { list.Add(random.Next(0, 100)); } Console.WriteLine(list.ToJSON()); using (FileStream fs = File.Open("list.txt", FileMode.Create)) { list.Serialize(fs); } using (FileStream fs = File.Open("list.txt", FileMode.Open)) { list1.Deserialize(fs); } Console.WriteLine(list1.ToJSON()); }
/// <summary> /// Generates a valid value. /// </summary> public T Generate() { T value = ListRandom.GenerateValid(Generator.Generate, Filter.MatchNewValue); Filter.RegisterValue(value); //Return. return(value); }
public Round(int unitCount, int maxTeam, int maxGroup) : base() { List <int> CommonFactor(int number) { List <int> cf = new List <int>(); for (int i = 2; i <= maxTeam; i++) { if (number % i == 0) { cf.Add(i); } } return(cf); } List <int> unitsCF = CommonFactor(unitCount); int groupCount = 1; if (unitCount >= 4) { int minGroup = unitsCF.Count > 1 && unitCount <= maxTeam ? 1 : 2; groupCount = Random.Range(minGroup, maxGroup + 1); } int groupRemain = unitCount; bool done = false; int repeatCount = 0; while (!done && repeatCount++ < 200) { int groupUnits = Random.Range(2, groupRemain + 1); List <int> groupUnitsCF = CommonFactor(groupUnits); if (groupUnitsCF.Any()) { int teamCount = ListRandom.In(groupUnitsCF); Group group = new Group(); for (int i = 0; i < teamCount; i++) { group.Add(new Team()); } group.eachTeamMember = groupUnits / teamCount; Add(group); groupRemain -= groupUnits; } if (groupRemain == 1 || Count > groupCount) { Clear(); groupRemain = unitCount; } done = Count <= groupCount && groupRemain == 0; } if (repeatCount > 200) { Debug.Log("fail"); } }
private HexTile selectHexTile(int u, int v) { List <HexTile> neighbours = new List <HexTile>(); for (int q = -1; q <= 1; q++) { for (int p = -1; p <= 1; p++) { // Skip ourselves and the two outliers. if (p != q) { Coordinate neighbour = new Coordinate(u + p, v + q); if (hexTiles.ContainsKey(neighbour)) { neighbours.Add(hexTiles[neighbour]); } } } } if (neighbours.Count > 0) { // This is like performing a weighted selection (since it is uniformly distributed) but WAY easier. HexTile neighbour = ListRandom.select(neighbours); // Now the actual weighted selection (see how much easier it is now); return(neighbour.selectHexTile()); } else { float totalWeight = 0.0f; foreach (HexTileWeight hexTileWeight in hexTileWeights) { totalWeight += hexTileWeight.weight; } float value = Random.value * totalWeight; float cumulativeWeight = 0.0f; foreach (HexTileWeight hexTileWeight in hexTileWeights) { cumulativeWeight += hexTileWeight.weight; if (value < cumulativeWeight) { return(hexTileWeight.hexTile); } } // We canna get here cap'n! return(null); } }
private void bDeserialize_Click(object sender, EventArgs e) { try { list = new ListRandom(); var fs = new FileStream("list.blf", FileMode.Open); list.Deserialize(fs); pictureBox1.Invalidate(); MessageBox.Show("list deserialization complete"); } catch (Exception ex) { MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public override void OnInitialize() { toEarth = Subworld.IsActive <Moon>(); lunaBackground = ModContent.GetTexture($"{nameof(Macrocosm)}/Content/Subworlds/LoadingBackgrounds/Luna"); earthBackground = ModContent.GetTexture($"{nameof(Macrocosm)}/Content/Subworlds/LoadingBackgrounds/Earth"); var textFileLoader = new TextFileLoader(); if (toEarth) { _chosenMessage = ListRandom.Pick(textFileLoader.Parse("Content/Subworlds/Earth/EarthMessages")); } else { _chosenMessage = ListRandom.Pick(textFileLoader.Parse("Content/Subworlds/Moon/MoonMessages")); } }
private ListRandom GetListRandom(int countNodes) { var list = new ListRandom(); for (int i = 0; i < countNodes; i++) { list.AddWithoutRandom(i.ToString()); } list.ForEachStoppable((ListNode node, ref bool stop) => { node.Random = list.Get(_random.Next(0, list.Count - 1)); }); return(list); }
static void Main(string[] args) { var node1 = new ListNode() { Data = "1" }; var node2 = new ListNode() { Data = "2" }; var node3 = new ListNode() { Data = "3" }; var node4 = new ListNode() { Data = "4" }; var firstList = new ListRandom(node1); firstList.Add(node2); firstList.Add(node3); firstList.Add(node4); using (FileStream fs = new FileStream("list.txt", FileMode.Create)) { firstList.Serialize(fs); } var secondList = new ListRandom(); using (FileStream fs = new FileStream("list.txt", FileMode.Open)) { secondList.Deserialize(fs); } Console.WriteLine("firstList \t secondList"); for (int i = 0; i < firstList.Count; i++) { Console.WriteLine("data:{0} \t {1}", firstList.Get(i).Data, secondList.Get(i).Data); Console.WriteLine("rand:{0} \t {1}", firstList.Get(i).Random.Data, secondList.Get(i).Random.Data); } Console.ReadLine(); }
public bool SerializeDeserializeTest(ListRandom list) { var buffSizeOfElement = 255; var streamBuffSize = buffSizeOfElement * list.Count; var stream = new MemoryStream(streamBuffSize); list.Serialize(stream); stream.Position = 0; var list2 = new ListRandom(); list2.Deserialize(stream); stream.Close(); return(list.Equals(list2)); }
public void RandomAssign(List <UnitInfo> units) { List <int> Ids = Enumerable.Range(0, units.Count).ToList(); foreach (Group group in this) { foreach (Team team in group) { for (int i = 0; i < group.eachTeamMember; i++) { int id = ListRandom.In(Ids); Ids.Remove(id); UnitInfo unit = units[id]; unit.team = team; team.unitInfos.Add(unit); } } } }
public TeeniGramGroup(int count, SuperstarOutput output = null, bool lastNames = false, double propability = 0.8d) { this.output = output; Random random = new Random(); ListRandom <string> nameRandom = new ListRandom <string>(names, random); users = Enumerable.Repeat(new User(), count).Select(x => new User { name = nameRandom.Next(), }).ToList(); if (!lastNames) { users.ForEach(x => x.name = x.name.Split(' ')[0]); } users.ForEach(user => { ListRandom <User> listRandom = new ListRandom <User>(users .Where(x => x.name != user.name) .ToList(), random); user.following = listRandom.Next(random.Next(0, listRandom.Count)).ToList(); }); if (random.NextDouble() < propability) { int pos = random.Next(0, users.Count); users.ForEach(delegate(User user) { if (!user.following.Contains(users[pos])) { user.following.Add(users[pos]); } }); users[pos].following.Clear(); } }
static void Main(string[] args) { Console.WriteLine("Enter the number of elements in the list"); var countStr = Console.ReadLine(); if (!int.TryParse(countStr, out var count)) { Console.WriteLine("It's not number"); return; } var listRandom = new List <(string collection, int indexes)>(); var rnd = new Random(); for (int i = 0; i < count; i++) { Console.WriteLine($"Enter the {i} element"); var data = Console.ReadLine(); listRandom.Add((data, rnd.Next(count))); } var list = new ListRandom(listRandom); Console.WriteLine(list.ToString()); using (var memStream = new MemoryStream()) { Console.WriteLine("Start Serialize list"); list.Serialize(memStream); Console.WriteLine("Finish Serialize list"); list = new ListRandom(new List <(string collection, int indexes)>()); Console.WriteLine("Start Deserialize list"); list.Deserialize(memStream); Console.WriteLine("Finish Deserialize list"); } Console.WriteLine(list.ToString()); }
public void Node_Random_Data_field_serialization_and_deserialization_check() { ListRandom list1 = ListRandom.GenerateRandomList(); Random rnd = new Random(); int randomNodeIndex = rnd.Next(1, list1.Count + 1); ListNode n = list1.Head; for (int i = 1; i < randomNodeIndex; i++) { n = n.Next; } string dataBeforeSerialization = n.Random.Data; var fs = new FileStream("test.blf", FileMode.Create); list1.Serialize(fs); var fs2 = new FileStream("test.blf", FileMode.Open); ListRandom list2 = new ListRandom(); list2.Deserialize(fs2); n = list2.Head; for (int i = 1; i < randomNodeIndex; i++) { n = n.Next; } string dataAfterSerialization = n.Random.Data; Assert.AreEqual(dataBeforeSerialization, dataAfterSerialization); }
public Vector3 getRandomPosition() { // Find a non-border hextile. HexTile hexTile; do { hexTile = ListRandom.select(new List <HexTile>(hexTiles.Values)); }while(hexTile.border); // Find the closest navigable point. Vector3?position = sampleSurface(hexTile.transform.position, radius * scale); if (position.HasValue) { return(position.Value); } else { Debug.Log("Could not sample NavMesh position."); return(hexTile.transform.position); } }
public PrepListNode(ListRandom listRandom) { ListRandom = listRandom; }
public string Build() { List <string> names = dataReader.ReadToList(); return(ListRandom.GetRandomElement(names)); }
public void Start() { // Calculate the closest bound. float maximumDistance = calculatePosition(uExtent, -vExtent).magnitude; for (int count = 0; count < numberOfCircles; count++) { Circle circle = new Circle(); circle.position = Random.insideUnitCircle * (maximumDistance - circleRadiusMaximum); circle.radius = Random.Range(circleRadiusMinimum, circleRadiusMaximum); circles.Add(circle); } // Shuffle the list of coordinates. List <Coordinate> coordinates = new List <Coordinate>(); for (int v = -vExtent; v <= vExtent; v++) { for (int u = -uExtent; u <= uExtent; u++) { coordinates.Add(new Coordinate(u, v)); } } ListRandom.shuffle(coordinates); // Place tiles. foreach (Coordinate coordinate in coordinates) { Vector3 position = calculatePosition(coordinate.u, coordinate.v); Quaternion rotation = Quaternion.identity; position.y = calculateHeight(coordinate.u, coordinate.v); if (accept(position)) { HexTile hexTile = Instantiate(selectHexTile(coordinate.u, coordinate.v), position, rotation, transform); hexTile.transform.localScale = new Vector3(scale, scale, scale); hexTile.name = "HexTile " + coordinate.u + " " + coordinate.v; hexTiles.Add(coordinate, hexTile); } } // Make borders. for (int count = 0; count < borderWidth; count++) { HashSet <Coordinate> borderCoordinates = new HashSet <Coordinate>(); foreach (Coordinate coordinate in hexTiles.Keys) { borderCoordinates.UnionWith(calculateBorder(coordinate.u, coordinate.v)); } foreach (Coordinate borderCoordinate in borderCoordinates) { Vector3 position = calculatePosition(borderCoordinate.u, borderCoordinate.v); Quaternion rotation = Quaternion.identity; position.y = calculateHeight(borderCoordinate.u, borderCoordinate.v); HexTile hexTile = Instantiate(selectBorderHexTile(borderCoordinate.u, borderCoordinate.v), position, rotation, transform); hexTile.transform.localScale = new Vector3(scale, scale, scale); hexTile.name = "Border HexTile " + borderCoordinate.u + " " + borderCoordinate.v; hexTile.border = true; hexTiles.Add(borderCoordinate, hexTile); } } // Build surface. generateSurface(); }
private void button1_Click(object sender, EventArgs e) { list = ListRandom.GenerateRandomList(); pictureBox1.Invalidate(); }
public CreateXmlDocument(ListRandom listrandom, string close) { ListRandom = listrandom; Close = close; }
/// <summary> /// Generates the random points that define this octave. /// </summary> /// <param name="pointCount">The amount of points.</param> /// <param name="rng">The random number generator used to generate the points.</param> /// <returns>The generated points.</returns> private static float[] GeneratePoints(int pointCount, IRandomGenerator rng) { return(ListRandom.GenerateRandom(() => rng.RandomRange(), pointCount).ToArray()); }