生成多个随机数
我想生成 25 个唯一的随机数并将它们列在控制台中。数字的长度应至少为 10 个字符。有什么简单的方法可以做到这一点吗?
I want to generate 25 unique random numbers and list them in a console. The numbers should be atleast 10 characters long. Any easy way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试将数字构建为字符串,并使用 HashSet 确保它们是唯一的:
示例输出:
类
HashSet
存在于 .NET 3.5 及更高版本中。Try building the numbers up as strings, and use a HashSet to ensure they are unique:
Example output:
The class
HashSet
is present in .NET 3.5 and newer.问题出在“25个唯一随机”上。显示 25 个随机数非常简单,
但这些数字不一定是唯一的。如果您不想允许重复,则需要以某种方式存储以前生成的数字,并在遇到旧数字时再次滚动。
请注意,您可以通过这种方式更改生成的数字的概率分布。
编辑:我刚刚注意到这些数字应该有十个字符长。由于 9,999,999,999 超过了 Int32.MaxValue,我建议使用
Math.Floor(r.NextDouble() * 10000000000 + 1000000000)
而不是r.Next(1,100)
。由于您的数字很长,因此您不必担心重复。他们是非常非常不可能的。
The problem lies a little in "25 unique random". Displaying 25 random numbers is as easy as
These are not necessarily unique, though. If you do not want to allow duplicates, you need to store previously generated numbers somehow, and roll again if you hit an old one.
Be aware that you change the probability distribution of your generated numbers this way.
Edit: I've just noticed that these numbers should be ten characters long. Since 9,999,999,999 exceeds Int32.MaxValue, I'd suggest using
Math.Floor(r.NextDouble() * 10000000000 + 1000000000)
instead ofr.Next(1,100)
.Since your numbers are that long, you should not need to worry about duplicates. They are very very unlikely.
随机性和唯一性之间有很大的不同。
因此,如果您确实需要唯一号码,则必须确保将所有已创建的号码保存在某个位置,并检查新创建的号码是否不在该列表中,或者您必须提供某种算法来确保给定的数字不能创建两次。
为了让第二部分工作,您主要采用创建时刻的日期/时间,因为当前日期/时间对永远是唯一的。唯一的问题是您每(毫秒)秒有多少个创作,以及有多少位数字可用于存储您的唯一编号。
有关使用 12 位数字的示例此处制作。希望这有帮助。
There is a big different between Randomness and Uniqueness.
So if you need really unique numbers you have to make sure that you save somewhere all already created numbers and check if your newly created one isn't within this list or you have to provide some algorithm that ensures that a given number can't created twice.
To get the second part to work you mostly take the date/time of the creation moment, cause the current date/time pair is unique forever. The only problem is how many creations per (milli)second do you have and how many digits are available to store your unique number.
A sample about using 12 digits is made here. Hope this helps.
一种简单的方法是:
这将确保数字始终为 10 个字符(数字)长。然而,它们不一定是独一无二的。如果您希望它们绝对是唯一的,则必须执行以下操作:
如果您希望能够拥有超过 10 位数字,则可以在 10 和最大数字位数之间随机生成数字位数。然后在 StringBuilder 中随机生成每个数字(或数字组) 或列表。您可以使用我上面使用的相同的 HashSet 方法来确保唯一性。
One simple way is this:
This will ensure that the numbers are always 10 characters (digits) long. They will not necessarily be unique however. If you want them to definitely be unique, you'll have to do something like this:
If you want to be able to have more than ten digits, you generate the number of digits randomly between 10 and your max number of digits. Then generate each digit (or group of digits) randomly in a StringBuilder or List. You can use the same HashSet method I used above to ensure uniqueness.