【发布时间】:2019-10-24 14:26:09
【问题描述】:
c# Generate Random number 传递 long 作为种子而不是 int32,但我需要传递电话号码或帐号
请推荐任何可靠的 NuGet 包,或者任何已经做过类似事情的实现。
我需要将完整的 PhoneNumber 作为种子传递,我可以在 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.
【问题讨论】:
-
没有构造函数接受
Int64种子。要么将所需的值作为种子转换为 Int32,要么使用 .NET Core 提供的 Random 类以外的其他东西。 -
我怀疑你是否理解
seed的用途。给定某个seed值,典型的随机生成器将根据该种子生成一个固定的数字序列。下次您使用相同的seed创建 Rnd 生成器对象时,将生成 相同的 FIXED 序列。无论使用int或long作为种子,这都是正确的。演示:dotnetfiddle.net/TQQm6l -
请记住,多个唯一的 long 值可以映射到同一个 int 值。至于转换,您可以这样做:
int seed = (int)(someLongValue % int.MaxValue); -
@inan 我的意思不是可能有重复的电话号码,而是两个唯一的电话号码可能映射到相同的种子值。假设我有 longA 和 longB,其中 longA != longB。但是,如果您计算它们各自的seedA 和seedB,那么seedA == seedB 可能会发生。此外,您还没有真正向我们提供有关在您的应用程序中使用它的方式和目标的任何信息,因此很难确定哪种方法会更好。