生成多个随机数

发布于 2024-09-03 05:49:33 字数 67 浏览 1 评论 0原文

我想生成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一场信仰旅途 2024-09-10 05:49:33

尝试将数字构建为字符串,并使用 HashSet 确保它们是唯一的:

Random random = new Random();
HashSet<string> ids = new HashSet<string>();

while (ids.Count < 25)
{
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 10; ++i)
    {
        sb.Append(random.Next(10));
    }
    ids.Add(sb.ToString());
}

示例输出:

7895499338
2643703497
0126762624
8623017810
...etc...

HashSet 存在于 .NET 3.5 及更高版本中。

Try building the numbers up as strings, and use a HashSet to ensure they are unique:

Random random = new Random();
HashSet<string> ids = new HashSet<string>();

while (ids.Count < 25)
{
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 10; ++i)
    {
        sb.Append(random.Next(10));
    }
    ids.Add(sb.ToString());
}

Example output:

7895499338
2643703497
0126762624
8623017810
...etc...

The class HashSet is present in .NET 3.5 and newer.

你好,陌生人 2024-09-10 05:49:33

问题出在“25个唯一随机”上。显示 25 个随机数非常简单,

Random r = new Random();
for(int i=0; i<25; i++)
    Console.WriteLine(r.Next(1,100).ToString());

但这些数字不一定是唯一的。如果您不想允许重复,则需要以某种方式存储以前生成的数字,并在遇到旧数字时再次滚动。

请注意,您可以通过这种方式更改生成的数字的概率分布。

编辑:我刚刚注意到这些数字应该有十个字符长。由于 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

Random r = new Random();
for(int i=0; i<25; i++)
    Console.WriteLine(r.Next(1,100).ToString());

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 of r.Next(1,100).

Since your numbers are that long, you should not need to worry about duplicates. They are very very unlikely.

笑叹一世浮沉 2024-09-10 05:49:33

随机性和唯一性之间有很大的不同。

因此,如果您确实需要唯一号码,则必须确保将所有已创建的号码保存在某个位置,并检查新创建的号码是否不在该列表中,或者您必须提供某种算法来确保给定的数字不能创建两次。

为了让第二部分工作,您主要采用创建时刻的日期/时间,因为当前日期/时间对永远是唯一的。唯一的问题是您每(毫秒)秒有多少个创作,以及有多少位数字可用于存储您的唯一编号。

有关使用 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.

呆头 2024-09-10 05:49:33

一种简单的方法是:

class Test
{
    private static void Main()
    {
        Random rand = new Random();

        for (int i = 0; i < 25; ++i)
        {
            Console.WriteLine(rand.Next(1000000000, int.MaxValue));
        }
    }
}

这将确保数字始终为 10 个字符(数字)长。然而,它们不一定是独一无二的。如果您希望它们绝对是唯一的,则必须执行以下操作:

class Test
{
    private static void Main()
    {
        Random rand = new Random();

        var generatedSoFar = new HashSet<int>();
        for (int i = 0; i < 25; ++i)
        {
            int newRand;
            do
            {
                newRand = rand.Next(1000000000, int.MaxValue);
            } while (generatedSoFar.Contains(newRand)); // generate a new random number until we get to one we haven't generated before

            generatedSoFar.Add(newRand);

            Console.WriteLine(newRand);
        }
    }
}

如果您希望能够拥有超过 10 位数字,则可以在 10 和最大数字位数之间随机生成数字位数。然后在 StringBuilder 中随机生成每个数字(或数字组) 列表。您可以使用我上面使用的相同的 HashSet 方法来确保唯一性。

One simple way is this:

class Test
{
    private static void Main()
    {
        Random rand = new Random();

        for (int i = 0; i < 25; ++i)
        {
            Console.WriteLine(rand.Next(1000000000, int.MaxValue));
        }
    }
}

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:

class Test
{
    private static void Main()
    {
        Random rand = new Random();

        var generatedSoFar = new HashSet<int>();
        for (int i = 0; i < 25; ++i)
        {
            int newRand;
            do
            {
                newRand = rand.Next(1000000000, int.MaxValue);
            } while (generatedSoFar.Contains(newRand)); // generate a new random number until we get to one we haven't generated before

            generatedSoFar.Add(newRand);

            Console.WriteLine(newRand);
        }
    }
}

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.

美人如玉 2024-09-10 05:49:33
 Random rnd = new Random(table);
 for(int i = 0; i < 25; ++i) {
   Console.WriteLine("{0}", rnd.Next(50, 50+i) 
 }
 Random rnd = new Random(table);
 for(int i = 0; i < 25; ++i) {
   Console.WriteLine("{0}", rnd.Next(50, 50+i) 
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文