public async Task <string> GetRandomPicture(string query) { if (string.IsNullOrWhiteSpace(query)) { return("Query is empty"); } List <string> pictures = new List <string>(); foreach (ISiteProvider siteProvider in SiteProviders) { try { List <string> currentPictures = await siteProvider.QueryByTagAsync(query, RandomMax); pictures.AddRange(currentPictures); } catch (Exception) { } } if (pictures.Count == 0) { return("No posts matched your search."); } string randomPicture = pictures[RandomThreadSafe.Next(0, pictures.Count)]; return(randomPicture); }
/// <summary> /// The world generation procedure /// </summary> /// <param name="world">The target world</param> /// <param name="progressBar">A progressBar instance to render progress upon</param> public void Generate(World world, ProgressBar progressBar = null) { progressBar.Value = 0; world.BlankMap(Field.IdealHeight, Field.IdealTemperature); var featureCount = RandomThreadSafe.Next((world.Width + world.Height) / 4, (world.Width + world.Height) * 2); var features = new List <IMapFeature>(); for (int i = 0; i < featureCount; i++) { features.Add(availableFeatures[RandomThreadSafe.Next(0, availableFeatures.Count)].CreateSelf(world)); } for (int i = 0; i < featureCount; i++) { features[i].Effect(); if (progressBar != null) { progressBar.Value = 100 * i / featureCount; } } if (progressBar != null) { progressBar.Value = 100; } }
/// <summary> /// Allows for construction of the feature actually affecting the world. The constructed feature is fully randomised /// </summary> /// <param name="world">The world to be affected</param> protected Feature(World world) { this.world = world; this.x = RandomThreadSafe.Next(0, world.Width); this.y = RandomThreadSafe.Next(0, world.Height); this.intensity = RandomThreadSafe.Next(1, 126); this.distance = RandomThreadSafe.Next(1, (this.world.Height + this.world.Width) / 4); }
public Task GetRandomAsync() { var dbSet = SQL.Set <Fact>(); RandomThreadSafe rts = new RandomThreadSafe(); int count = dbSet.Count(); int skipN = rts.Next(count); Reply(SQL.Set <Fact>().Skip(skipN).FirstOrDefault()); return(Task.CompletedTask); }
/// <summary> /// Mutates the neuron's values /// </summary> /// <param name="mutability">The mutation rate</param> public void Mutate(double mutability) { coeficient += RandomThreadSafe.NextDouble(-mutability, mutability); coeficient = Math.Max(1, coeficient); coeficient = Math.Min(0, coeficient); mutability += RandomThreadSafe.NextDouble(-mutability, mutability); mutability = Math.Max(1, mutability); mutability = Math.Min(0, mutability); foreach (var output in outputs) { output.Mutate(mutability); } }
/// <summary> /// An in-place shuffling method for any list /// </summary> /// <typeparam name="T">The type contained within our list</typeparam> /// <param name="list">The list to be shuffled</param> public static void Shuffle <T>(this IList <T> list) { int n = list.Count; while (n > 1) { n--; int k = RandomThreadSafe.Next(n + 1); T value = list[k]; list[k] = list[n]; list[n] = value; } }
protected River(World world) : base(world) { //The river is different in that it moves in only one direction var direction = RandomThreadSafe.Next(8); canMoveMinusX = false; canMoveMinusY = false; canMovePlusX = false; canMovePlusY = false; switch (direction) { case 0: canMoveMinusX = true; break; case 1: canMoveMinusY = true; break; case 2: canMovePlusX = true; break; case 3: canMovePlusY = true; break; case 4: canMoveMinusX = true; canMoveMinusY = true; break; case 5: canMoveMinusX = true; canMovePlusY = true; break; case 6: canMovePlusX = true; canMovePlusY = true; break; case 7: canMoveMinusY = true; canMovePlusX = true; break; default: throw new IndexOutOfRangeException("Direction out of bounds"); } }
/// <summary> /// Handles the spread of the Feature instance's effect based on the allowed directions it can take /// </summary> protected virtual void EffectSpread() { //List of 8 possible ways is prepared, but filled only as needed var children = new List <IMapFeature>(8); if (canMoveMinusX) { children.Add(CreateSelf(world, x - 1, y, intensity - RandomThreadSafe.Next(1, intensity / 2 + 1), distance - RandomThreadSafe.Next(0, distance / 2 + 1), false, true, false, false)); if (canMoveMinusY) { children.Add(CreateSelf(world, x - 1, y - 1, intensity - RandomThreadSafe.Next(1, intensity / 2 + 1), distance - RandomThreadSafe.Next(0, distance / 2 + 1), false, true, false, true)); } if (canMovePlusY) { children.Add(CreateSelf(world, x - 1, y + 1, intensity - RandomThreadSafe.Next(1, intensity / 2 + 1), distance - RandomThreadSafe.Next(0, distance / 2 + 1), false, true, false, true)); } } if (canMovePlusX) { children.Add(CreateSelf(world, x + 1, y, intensity - RandomThreadSafe.Next(1, intensity / 2 + 1), distance - RandomThreadSafe.Next(0, distance / 2 + 1), true, false, false, false)); if (canMoveMinusY) { children.Add(CreateSelf(world, x + 1, y - 1, intensity - RandomThreadSafe.Next(1, intensity / 2 + 1), distance - RandomThreadSafe.Next(0, distance / 2 + 1), true, false, false, true)); } if (canMovePlusY) { children.Add(CreateSelf(world, x + 1, y + 1, intensity - RandomThreadSafe.Next(1, intensity / 2 + 1), distance - RandomThreadSafe.Next(0, distance / 2 + 1), true, false, true, false)); } } if (canMoveMinusY) { children.Add(CreateSelf(world, x, y - 1, intensity - RandomThreadSafe.Next(1, intensity / 2 + 1), distance - RandomThreadSafe.Next(0, distance / 2 + 1), false, false, false, true)); } if (canMovePlusY) { children.Add(CreateSelf(world, x, y + 1, intensity - RandomThreadSafe.Next(1, intensity / 2 + 1), distance - RandomThreadSafe.Next(0, distance / 2 + 1), false, false, true, false)); } foreach (var child in children) { child.Effect(); } }
// Get a random picture from e6. public async Task <string> GetRandomPicture(string tags) { if (tags.Count() > _max_search_query_character_size) { return($"Character limit of {_max_search_query_character_size} exceeded! You have input ${tags.Count()} characters."); } List <e6Post> e6Posts = await Querye6ByTag(tags, _query_limit); List <int> ids = e6Posts.Select(x => x.id).ToList(); if (ids.Count() == 0) { return("No posts matched your search."); } int random_index = RandomThreadSafe.Next(0, ids.Count()); string return_string = $"{_e6_show_post_base_url}/{ids[random_index]}"; return(return_string); }
/// <summary> /// Generates a random brain /// </summary> public void GenerateRandom() { if (Generated) { throw new InvalidOperationException("Brain already generated!"); } for (int i = 0; i < inputNeurons.Length; i++) { inputNeurons[i] = new SensoryNeuron(); } for (int i = 0; i < thinkingNeurons.Length; i++) { thinkingNeurons[i] = new Neuron(); } for (int i = 0; i < outputNeurons.Length; i++) { outputNeurons[i] = new Neuron(); } for (int i = 0; i < memory.Length; i++) { memory[i] = RandomThreadSafe.NextDouble(); } //Not the true max, as we do not limit multiple synapses between the same neurons, but it is the count of possible unique ones var MaxSynapsesBetweenLayers = inputNeurons.Length * thinkingNeurons.Length; for (int i = 0; i < RandomThreadSafe.Next(MaxSynapsesBetweenLayers); i++) { var fromIndex = RandomThreadSafe.Next(inputNeurons.Length); var toIndex = RandomThreadSafe.Next(thinkingNeurons.Length); var synapse = new Synapse(inputNeurons[fromIndex], thinkingNeurons[toIndex], RandomThreadSafe.NextDouble()); } MaxSynapsesBetweenLayers = thinkingNeurons.Length * outputNeurons.Length; for (int i = 0; i < RandomThreadSafe.Next(MaxSynapsesBetweenLayers); i++) { var fromIndex = RandomThreadSafe.Next(thinkingNeurons.Length); var toIndex = RandomThreadSafe.Next(outputNeurons.Length); var synapse = new Synapse(thinkingNeurons[fromIndex], outputNeurons[toIndex], RandomThreadSafe.NextDouble()); } Generated = true; }
/// <summary> /// Generates the tiles of a world /// </summary> /// <param name="world">The world to affect</param> /// <param name="progressBar">A progress bar to render the progress upon</param> public void Generate(World world, ProgressBar progressBar = null) { world.BlankMap(0, 0); var passes = RandomThreadSafe.Next((world.Width + world.Height) / 2, (world.Width + world.Height)); var toAgitate = new List <Coordinate>(); //For a random ammount of passes for (int i = 0; i < passes; i++) { var drops = RandomThreadSafe.Next(1, (world.Width + world.Height)); toAgitate.Clear(); //Do a random ammount of drops based on the field size for (int j = 0; j < drops; j++) { var x = RandomThreadSafe.Next(0, world.Width); var y = RandomThreadSafe.Next(0, world.Height); //Drop a random ammount of height and / or temperature particles on a given field var doHeight = RandomThreadSafe.Next(0, 2); var doTemp = RandomThreadSafe.Next(0, 2); world.Fields[x][y].Drop(RandomThreadSafe.Next(64, 255) * doHeight, RandomThreadSafe.Next(64, Field.MaxValue) * doTemp); toAgitate.Add(new Coordinate(x, y)); } foreach (var item in toAgitate) { AgitateDrop(world, item.x, item.y); } if (progressBar != null) { progressBar.Value = (i + 1) * 100 / passes; } } if (progressBar != null) { progressBar.Value = 100; } }
/// <summary> /// Mutates the neuron's synapses /// </summary> /// <param name="mutability">Mutation rate</param> /// <param name="targetNeurons">Possible neurons to connect new synapses to</param> public void MutateSynapses(double mutability, Neuron[] targetNeurons) { var synapsesToDie = new List <Synapse>(); foreach (var synapse in outputs) { synapse.Mutate(mutability); if (RandomThreadSafe.NextDouble() < mutability) { synapsesToDie.Add(synapse); } } foreach (var synapseToDie in synapsesToDie) { outputs.Remove(synapseToDie); synapseToDie.To.inputs.Remove(synapseToDie); } if (RandomThreadSafe.NextDouble() < mutability) { var possibleTarget = targetNeurons[RandomThreadSafe.Next(0, targetNeurons.Length)]; var newSynapse = new Synapse(this, possibleTarget, RandomThreadSafe.NextDouble()); } }
/// <summary> /// Mutates the synapse weight /// </summary> /// <param name="mutability">The mutation rate</param> public void Mutate(double mutability) { Weight += RandomThreadSafe.NextDouble(-mutability, mutability); Weight = Math.Max(1, Weight); Weight = Math.Min(0, Weight); }
/// <summary> /// Constructs a random neuron /// </summary> public Neuron() { coeficient = RandomThreadSafe.NextDouble(); }