You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
895 B

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace SqlSugar
  5. {
  6. public class UtilRandom
  7. {
  8. public static Random Random = new Random();
  9. public static int GetRandomIndex(Dictionary<int, int> pars)
  10. {
  11. var maxValue = 0;
  12. foreach (var item in pars)
  13. {
  14. maxValue += item.Value;
  15. }
  16. var num = Random.Next(1, maxValue);
  17. var result = 0;
  18. var endValue = 0;
  19. foreach (var item in pars)
  20. {
  21. var index = pars.ToList().IndexOf(item);
  22. var beginValue = index == 0 ? 0 : pars[index - 1];
  23. endValue += item.Value;
  24. result = item.Key;
  25. if (num >= beginValue && num <= endValue)
  26. break;
  27. }
  28. return result;
  29. }
  30. }
  31. }