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
32 lines
895 B
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace SqlSugar
|
|
{
|
|
public class UtilRandom
|
|
{
|
|
public static Random Random = new Random();
|
|
public static int GetRandomIndex(Dictionary<int, int> pars)
|
|
{
|
|
var maxValue = 0;
|
|
foreach (var item in pars)
|
|
{
|
|
maxValue += item.Value;
|
|
}
|
|
var num = Random.Next(1, maxValue);
|
|
var result = 0;
|
|
var endValue = 0;
|
|
foreach (var item in pars)
|
|
{
|
|
var index = pars.ToList().IndexOf(item);
|
|
var beginValue = index == 0 ? 0 : pars[index - 1];
|
|
endValue += item.Value;
|
|
result = item.Key;
|
|
if (num >= beginValue && num <= endValue)
|
|
break;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|