随机化一个 BigInteger
我正在寻找随机化一个 BigInteger。目的是从 1 到 8180385048 中选择一个数字。虽然,据我观察,BigInteger(BitLen, Random) 是从 n 到 X2-1,但我想要一些不可预测的数字。我试图制作一种可以做到这一点的方法,但我不断遇到错误,并最终屈服于在这里询问。 :P 有人对如何做到这一点有任何建议吗?
I'm looking to randomize a BigInteger. The intent is to pick a number from 1 to 8180385048. Though, from what I noticed, the BigInteger(BitLen, Random) does it from n to X2-1, I'd want some unpredictable number. I tried to make a method that would do it, but I keep running into bugs and have finally given in to asking on here. :P Does anyone have any suggestions on how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从
Random.nextInt(int n)
显然需要解决同样的问题,他们似乎得出的结论是,你不能比“超出范围时重新采样”做得更好,但是预计处罚可以忽略不计。来自文档:
建议您只需使用 随机化构造函数并迭代,直到达到范围内的值,例如如下所示:
对于您的特定情况(最大值= 8180385048),必须的概率重申一次,大约是 4.8%,所以不用担心:-)
Judging from the docs of
Random.nextInt(int n)
which obviously needs to solve the same problem, they seem to have concluded that you can't do better than "resampling if out of range", but that the penalty is expected to be negligible.From the docs:
I'd suggest you simply use the randomizing constructor you mentioned and iterate until you reach a value that is in range, for instance like this:
For your particular case (with max = 8180385048), the probability of having to reiterate, even once, is about 4.8 %, so no worries :-)
进行一个循环并随机获取涵盖范围的最小位长度的 BigInteger,直到获得范围内的一个数字。这应该保留随机数的分布。
Make a loop and get random
BigInteger
s of the minimum bit length that covers your range until you obtain one number in range. That should preserve the distribution of random numbers.正如其他答案中所建议的,如果超出范围,则重申是解决此问题的方法。但是,如果您想避免这种情况,另一种选择是使用模运算符:
Reiterating if out of range, as suggested in other answers, is a solution to this problem. However if you want to avoid this, another option is to use the modulus operator: