简单来讲就是先生成字符串的md5,然后再用md5的前几位转换为Int作为随机种子
废话不多说直接贴代码:
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace Kakkk
{
class Program
{
static void Main(string[] args)
{
string str = "emmmmm";
Random random = new Random(GetSeedFromString(str));
Console.WriteLine(random.Next());
}
//获取随机种子
public static int GetSeedFromString(string value)
{
using (var md5 = MD5.Create())
{
var inputBytes = Encoding.UTF8.GetBytes(value);
byte[] hashBytes = md5.ComputeHash(inputBytes);
return BitConverter.ToInt32(hashBytes, 0);
}
}
}
}