Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- f(weight)
- fruits = [apple, apple, apple, apple, orange, orange, lemon]
- intervals = [3, 5, 6]
- public static T Random<T>(this IEnumerable<T> enumerable, Func<T, int> weightFunc)
- {
- int totalWeight = 0; // this stores sum of weights of all elements before current
- T selected = default(T); // currently selected element
- foreach (var data in enumerable)
- {
- int weight = weightFunc(data); // weight of current element
- int r = Random.Next(totalWeight + weight); // random value
- if (r >= totalWeight) // probability of this is weight/(totalWeight+weight)
- selected = data; // it is the probability of discarding last selected element and selecting current one instead
- totalWeight += weight; // increase weight sum
- }
- return selected; // when iterations end, selected is some element of sequence.
- }
- +------------------------+
- | fruit | weight | csum |
- +------------------------+
- | apple | 4 | 4 |
- | orange | 2 | 6 |
- | lemon | 1 | 7 |
- +------------------------+
- double r = Random.Next() * totalSum;
- for(int i = 0; i < fruit.Count; i++)
- {
- if (csum[i] > r)
- return fruit[i];
- }
- double r = Random.Next() * totalSum;
- int lowGuess = 0;
- int highGuess = fruit.Count - 1;
- while (highGuess >= lowGuess)
- {
- int guess = (lowGuess + highGuess) / 2;
- if ( csum[guess] < r)
- lowGuess = guess + 1;
- else if ( csum[guess] - weight[guess] > r)
- highGuess = guess - 1;
- else
- return fruit[guess];
- }
- from random import random
- def select(container, weights):
- total_weight = float(sum(weights))
- rel_weight = [w / total_weight for w in weights]
- # Probability for each element
- probs = [sum(rel_weight[:i + 1]) for i in range(len(rel_weight))]
- slot = random()
- for (i, element) in enumerate(container):
- if slot <= probs[i]:
- break
- return element
- population = ['apple','orange','lemon']
- weights = [4, 2, 1]
- print select(population, weights)
- public static Random random = new Random(DateTime.Now.Millisecond);
- public int chooseWithChance(params int[] args)
- {
- /*
- * This method takes number of chances and randomly chooses
- * one of them considering their chance to be choosen.
- * e.g.
- * chooseWithChance(0,99) will most probably (%99) return 1
- * chooseWithChance(99,1) will most probably (%99) return 0
- * chooseWithChance(0,100) will always return 1.
- * chooseWithChance(100,0) will always return 0.
- * chooseWithChance(67,0) will always return 0.
- */
- int argCount = args.Length;
- int sumOfChances = 0;
- for (int i = 0; i < argCount; i++) {
- sumOfChances += args[i];
- }
- double randomDouble = random.NextDouble() * sumOfChances;
- while (sumOfChances > randomDouble)
- {
- sumOfChances -= args[argCount -1];
- argCount--;
- }
- return argCount-1;
- }
- string[] fruits = new string[] { "apple", "orange", "lemon" };
- int choosenOne = chooseWithChance(98,1,1);
- Console.WriteLine(fruits[choosenOne]);
- Console.WriteLine("Start...");
- int flipCount = 100;
- int headCount = 0;
- int tailsCount = 0;
- for (int i=0; i< flipCount; i++) {
- if (chooseWithChance(50,50) == 0)
- headCount++;
- else
- tailsCount++;
- }
- Console.WriteLine("Head count:"+ headCount);
- Console.WriteLine("Tails count:"+ tailsCount);
- Start...
- Head count:52
- Tails count:48
Add Comment
Please, Sign In to add comment