Guest User

Untitled

a guest
Jul 20th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. f(weight)
  2.  
  3. fruits = [apple, apple, apple, apple, orange, orange, lemon]
  4.  
  5. intervals = [3, 5, 6]
  6.  
  7. public static T Random<T>(this IEnumerable<T> enumerable, Func<T, int> weightFunc)
  8. {
  9. int totalWeight = 0; // this stores sum of weights of all elements before current
  10. T selected = default(T); // currently selected element
  11. foreach (var data in enumerable)
  12. {
  13. int weight = weightFunc(data); // weight of current element
  14. int r = Random.Next(totalWeight + weight); // random value
  15. if (r >= totalWeight) // probability of this is weight/(totalWeight+weight)
  16. selected = data; // it is the probability of discarding last selected element and selecting current one instead
  17. totalWeight += weight; // increase weight sum
  18. }
  19.  
  20. return selected; // when iterations end, selected is some element of sequence.
  21. }
  22.  
  23. +------------------------+
  24. | fruit | weight | csum |
  25. +------------------------+
  26. | apple | 4 | 4 |
  27. | orange | 2 | 6 |
  28. | lemon | 1 | 7 |
  29. +------------------------+
  30.  
  31. double r = Random.Next() * totalSum;
  32. for(int i = 0; i < fruit.Count; i++)
  33. {
  34. if (csum[i] > r)
  35. return fruit[i];
  36. }
  37.  
  38. double r = Random.Next() * totalSum;
  39. int lowGuess = 0;
  40. int highGuess = fruit.Count - 1;
  41.  
  42. while (highGuess >= lowGuess)
  43. {
  44. int guess = (lowGuess + highGuess) / 2;
  45. if ( csum[guess] < r)
  46. lowGuess = guess + 1;
  47. else if ( csum[guess] - weight[guess] > r)
  48. highGuess = guess - 1;
  49. else
  50. return fruit[guess];
  51. }
  52.  
  53. from random import random
  54.  
  55. def select(container, weights):
  56. total_weight = float(sum(weights))
  57. rel_weight = [w / total_weight for w in weights]
  58.  
  59. # Probability for each element
  60. probs = [sum(rel_weight[:i + 1]) for i in range(len(rel_weight))]
  61.  
  62. slot = random()
  63. for (i, element) in enumerate(container):
  64. if slot <= probs[i]:
  65. break
  66.  
  67. return element
  68.  
  69. population = ['apple','orange','lemon']
  70. weights = [4, 2, 1]
  71.  
  72. print select(population, weights)
  73.  
  74. public static Random random = new Random(DateTime.Now.Millisecond);
  75. public int chooseWithChance(params int[] args)
  76. {
  77. /*
  78. * This method takes number of chances and randomly chooses
  79. * one of them considering their chance to be choosen.
  80. * e.g.
  81. * chooseWithChance(0,99) will most probably (%99) return 1
  82. * chooseWithChance(99,1) will most probably (%99) return 0
  83. * chooseWithChance(0,100) will always return 1.
  84. * chooseWithChance(100,0) will always return 0.
  85. * chooseWithChance(67,0) will always return 0.
  86. */
  87. int argCount = args.Length;
  88. int sumOfChances = 0;
  89.  
  90. for (int i = 0; i < argCount; i++) {
  91. sumOfChances += args[i];
  92. }
  93.  
  94. double randomDouble = random.NextDouble() * sumOfChances;
  95.  
  96. while (sumOfChances > randomDouble)
  97. {
  98. sumOfChances -= args[argCount -1];
  99. argCount--;
  100. }
  101.  
  102. return argCount-1;
  103. }
  104.  
  105. string[] fruits = new string[] { "apple", "orange", "lemon" };
  106. int choosenOne = chooseWithChance(98,1,1);
  107. Console.WriteLine(fruits[choosenOne]);
  108.  
  109. Console.WriteLine("Start...");
  110. int flipCount = 100;
  111. int headCount = 0;
  112. int tailsCount = 0;
  113.  
  114. for (int i=0; i< flipCount; i++) {
  115. if (chooseWithChance(50,50) == 0)
  116. headCount++;
  117. else
  118. tailsCount++;
  119. }
  120.  
  121. Console.WriteLine("Head count:"+ headCount);
  122. Console.WriteLine("Tails count:"+ tailsCount);
  123.  
  124. Start...
  125. Head count:52
  126. Tails count:48
Add Comment
Please, Sign In to add comment