首页 > 解决方案 > c#生成随机数作为种子而不是int32传递

问题描述

c#生成随机数作为种子而不是int32传递,但我需要传递电话号码或帐号

https://docs.microsoft.com/en-us/dotnet/api/system.random.-ctor?view=netframework-4.8#System_Random__ctor_System_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.

标签: c#.net.net-core

解决方案


取电话号码的哈希值,例如:

var phoneNumber = 123456789L;
var seed = phoneNumber.GetHashCode();

这意味着对于相同的电话号码,您将获得相同的序列。这也意味着对于某些电话号码,您将获得相同的序列,但这会很渺茫。正如所评论的那样,在不同的 .net 运行时可能会有所不同,但您可能不在乎。

不知道你为什么想要,但我有原因,例如测试代码


推荐阅读