c# - c#生成随机数作为种子而不是int32传递
问题描述
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 运行时可能会有所不同,但您可能不在乎。
不知道你为什么想要,但我有原因,例如测试代码
推荐阅读
- python - 尝试通过列表传递for循环时出现索引超出范围错误
- rxjs - rxjs:我可以在定义 observable 之前订阅吗?
- typescript - 定义和使用固定数组类型
- javascript - 减少循环或代码可读性问题的数量
- python - 无法将迭代的字典列表发布到 api 端点,只发送列表中的第一个字典
- asp.net-core - 如何使用 OAuth 和 .Net Core Identity 对移动客户端进行身份验证?
- javascript - 从 Firefox 的地址栏和搜索栏获取输入的文本(不是 url)
- python - 蓝牙模块 discover_devices 不起作用
- cypress - 赛普拉斯自定义命令无法识别,随机失败
- python - 操作数据框以创建多个时间序列列