using System;
using System.Collections.Generic;
using System.Text;
namespace Exam.BLL
{
/// <summary>
/// 帮助生成随机数类
/// </summary>
class RandomNumberHelper
{
/// <summary>
/// 从指定列表中随机取出指定个数整数以新列表返回
/// </summary>
/// <param name="sourceList">原列表</param>
/// <param name="selectCount">要选取个数</param>
/// <returns>新列表</returns>
public static IList<int> RandomSelect(IList<int> sourceList,int selectCount)
{
if (selectCount > sourceList.Count)
throw new ArgumentOutOfRangeException("selectCount必需大于sourceList.Count");
IList<int> resultList = new List<int>();
for (int i = 0; i < selectCount; i++)
{
int nextIndex = GetRandomNumber(1, sourceList.Count);
int nextNumber = sourceList[nextIndex-1];
sourceList.RemoveAt(nextIndex-1);
resultList.Add(nextNumber);
}
return resultList;
}
/// <summary>
/// 生成一个整数大于等于最小值,小于等于最大值
/// </summary>
/// <param name="minValue">最小值</param>
/// <param name="maxValue">最大值</param>
/// <returns>整数,大于等于最小值,小于等于最大值</returns>
public static int GetRandomNumber(int minValue, int maxValue)
{
return random.Next(minValue, maxValue+1);
}
private static Random random = new Random();
}
}
using System.Collections.Generic;
using System.Text;
namespace Exam.BLL
{
/// <summary>
/// 帮助生成随机数类
/// </summary>
class RandomNumberHelper
{
/// <summary>
/// 从指定列表中随机取出指定个数整数以新列表返回
/// </summary>
/// <param name="sourceList">原列表</param>
/// <param name="selectCount">要选取个数</param>
/// <returns>新列表</returns>
public static IList<int> RandomSelect(IList<int> sourceList,int selectCount)
{
if (selectCount > sourceList.Count)
throw new ArgumentOutOfRangeException("selectCount必需大于sourceList.Count");
IList<int> resultList = new List<int>();
for (int i = 0; i < selectCount; i++)
{
int nextIndex = GetRandomNumber(1, sourceList.Count);
int nextNumber = sourceList[nextIndex-1];
sourceList.RemoveAt(nextIndex-1);
resultList.Add(nextNumber);
}
return resultList;
}
/// <summary>
/// 生成一个整数大于等于最小值,小于等于最大值
/// </summary>
/// <param name="minValue">最小值</param>
/// <param name="maxValue">最大值</param>
/// <returns>整数,大于等于最小值,小于等于最大值</returns>
public static int GetRandomNumber(int minValue, int maxValue)
{
return random.Next(minValue, maxValue+1);
}
private static Random random = new Random();
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步