随机种子参考文章:https://blog.csdn.net/conmajia/article/details/7596149
static void Main(string[] args)
{
Random rd = new Random();
List<int> rodomList = new List<int>();
rodomList.Clear();
for (int i = 0; i < 4;)
{
int n = rd.Next(10);
if (!rodomList.Contains(n))
{
rodomList.Add(n);
i++;
Console.WriteLine(n + " " + rodomList.Count);
}
}
}
升级版:
List<int> list = new List<int>(); //1到10随机4个
int[] numArry = new int[4];
for (int i = 0; i < 11; i++)
{
list.Add(i);
}
for (int i = 0; i < 4; i++)
{
int n = Random.Range(1, list.Count);
numArry[i] = list[n];
list.RemoveAt(n); //按索引号删除 空间换时间
}
foreach (var item in numArry)
{
print("*:"+item);
}
private static int randomCount = 0;
private static string CreateRandomText()
{
randomCount++;
Guid guid = Guid.NewGuid( );
int key1 = guid.GetHashCode( );
int key2 = unchecked( (int)DateTime.Now.Ticks );
int seed = unchecked( key1 * key2 * randomCount );
Random rand = new Random( seed );
int n = rand.Next( 100000, 999999 );
// (业务代码,略)...guid*time.ticks
}