Advertisement
Guest User

PseudoRandom

a guest
Feb 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1.  
  2. static int max = 3;
  3. static double[] chances = null;
  4.  
  5. internal static int Choose(this List<MenuSceneInfo> list, int hash)
  6. {
  7. if (chances == null)
  8. {
  9. chances = new double[list.Count];
  10. for (int i = 0; i < chances.Length; i++)
  11. {
  12. chances[i] = 100d / chances.Length;
  13. }
  14. }
  15.  
  16. double random = Math.Pow(hash, 0.5) % 100d;
  17. double sum = 0;
  18.  
  19. for (int i = 0; i < chances.Length; i++)
  20. {
  21. sum += chances[i];
  22. if (random < sum)
  23. {
  24. double penalty = Math.Min(chances[i], (100d / max) / chances.Length);
  25. double bonus = penalty / (chances.Length - 1);
  26.  
  27. for (int j = 0; j < chances.Length; j++)
  28. {
  29. chances[j] += bonus;
  30. }
  31.  
  32. chances[i] = chances[i] - bonus - penalty;
  33. return i;
  34. }
  35. }
  36.  
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement