Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#生成作为种子而不是int32传递的长随机数_C#_.net_.net Core - Fatal编程技术网

c#生成作为种子而不是int32传递的长随机数

c#生成作为种子而不是int32传递的长随机数,c#,.net,.net-core,C#,.net,.net Core,c#生成以种子形式长时间传递的随机数,而不是int32,但我需要传递电话号码或帐号 请推荐任何可靠的NuGet软件包,或者任何已经做过类似工作的实现 我需要将完整的电话号码作为种子进行传递,这在python中是可以做到的,但在C#中是不行的,我的代码栈都是C语言的# 使用制度 public class Program { public static void Main() { int seed = 0123456789; Rand

c#生成以种子形式长时间传递的随机数,而不是int32,但我需要传递电话号码或帐号

请推荐任何可靠的NuGet软件包,或者任何已经做过类似工作的实现

我需要将完整的电话号码作为种子进行传递,这在python中是可以做到的,但在C#中是不行的,我的代码栈都是C语言的#

使用制度

public class Program
{
    public static void Main()
    {
            int seed = 0123456789;
            Random random = new Random(seed);
            double result = random.NextDouble();
            Console.WriteLine(result);
    }
}
关于我的要求和我努力实现的目标的一些见解:

1)We're doing this for A/B testing and todo data analysis on the 
  experience of two services. 

2)When a request comes with
  phoneNumber based on random.NextDouble() there is a preset percentage
  which we use to determine whether to send a request to service A or
  service B 

3)For example, let's says the request comes and falls
 under >0.5 then we direct the request to service A and the next time
 the request with the same phone number comes in it will be >0.5 and
 goes service A since the seed is a unique hash of phoneNumber.

取电话号码的散列,例如:

var phoneNumber = 123456789L;
var seed = phoneNumber.GetHashCode();
这意味着对于相同的电话号码,您将获得相同的序列。这也意味着,对于某些电话号码,您将获得相同的序列,但这将是苗条的。正如所评论的,在不同的.net运行时上可能会有所不同,但您可能并不在意

我不知道为什么要这样做,但我有一些原因,例如测试代码GetHashCode()方法属于对象类,它与随机数生成无关。请阅读这里()。文档清楚地表明,如果输入是一致的,就有可能发生冲突

HashAlgorithm.ComputeHash(此处有文档记录)方法计算给定值的哈希,但本质上是一致的,即如果输入相同,则生成的输出也相同。显然,这不是期望的输出(我假设)。我已经附上了我试图生成的示例代码

static void Main(string[] args)
{

    Console.WriteLine("Hello World!");

    while (true)
    {
        Console.WriteLine("Enter a 9 digit+ number to calculate hash");
        var val = Console.ReadLine();
        long target = 0;
        bool result = long.TryParse(val,out target);
        if (result)
        {
            var calculatedHash = OutputHash(target);

            Console.WriteLine("Calculated hash is : " + calculatedHash);
        }
        else
        {
            Console.WriteLine("Incorrect input. Please try again.");
        }
    }
}

public static string OutputHash(long number)
{
    string source = Convert.ToString(number);
    string hash;

    using (SHA256 sha256Hash = SHA256.Create())
    {
        hash = GetHash(sha256Hash, source);

        Console.WriteLine($"The SHA256 hash of {source} is: {hash}.");

        Console.WriteLine("Verifying the hash...");

        if (VerifyHash(sha256Hash, source, hash))
        {
            Console.WriteLine("The hashes are the same.");
        }
        else
        {
            Console.WriteLine("The hashes are not same.");
        }
    }

    return hash;
}

private static string GetHash(HashAlgorithm hashAlgorithm, string input)
{

    // Convert the input string to a byte array and compute the hash.
    byte[] data = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(input));

    // Create a new Stringbuilder to collect the bytes
    // and create a string.
    var sBuilder = new StringBuilder();

    // Loop through each byte of the hashed data 
    // and format each one as a hexadecimal string.
    for (int i = 0; i < data.Length; i++)
    {
        sBuilder.Append(data[i].ToString("x2"));
    }

    // Return the hexadecimal string.
    return sBuilder.ToString();
}

// Verify a hash against a string.
private static bool VerifyHash(HashAlgorithm hashAlgorithm, string input, string hash)
{
    // Hash the input.
    var hashOfInput = GetHash(hashAlgorithm, input);

    // Create a StringComparer an compare the hashes.
    StringComparer comparer = StringComparer.OrdinalIgnoreCase;

    return comparer.Compare(hashOfInput, hash) == 0;
}
应该这样做()。 文档()说,即使这样,最终也会发生碰撞,但发生碰撞的可能性非常小


最后,这听起来像是

的潜在副本,没有构造函数接受
Int64
种子。要么将作为种子的值转换为Int32,要么使用.NET Core提供的随机类以外的其他类。我怀疑您是否理解
种子的作用。给定某个
种子
值,典型的随机生成器将基于该种子生成一个固定的数字序列。下次使用相同的
种子创建Rnd生成器对象时,将生成相同的固定序列。无论对种子使用
int
还是
long
,这都是正确的。演示:请记住,多个唯一的长值可以映射到同一个int值。对于转换,您可以这样做:
intseed=(int)(someLongValue%int.MaxValue)@inan我的观点不是可能有重复的电话号码,而是两个唯一的电话号码可能映射到相同的种子值。假设我有龙卡和龙卡,龙卡在哪里!=朗布。但是,如果您计算它们各自的seedA和seedB,那么可能会出现seedA==seedB。此外,您还没有向我们提供任何关于如何在您的应用程序中使用此方法以及使用此方法的目标的信息,因此很难确定哪种方法会更好。如果需要使用此方法来预测RNG的结果,则哈希不能保证跨框架版本产生相同的结果,您对如何操作有一些看法(以及不该做什么)使用散列。
Guid g = Guid.NewGuid();