简体   繁体   English

生成多个随机数

[英]Generating multiple random numbers

I want to generate 25 unique random numbers and list them in a console. 我想生成25个唯一的随机数,并将它们列在控制台中。 The numbers should be atleast 10 characters long. 这些数字至少应为10个字符。 Any easy way to do that? 有什么简单的方法吗?

Try building the numbers up as strings, and use a HashSet to ensure they are unique: 尝试将数字构建为字符串,并使用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());
}

Example output: 示例输出:

7895499338
2643703497
0126762624
8623017810
...etc...

The class HashSet is present in .NET 3.5 and newer. HashSet存在于.NET 3.5及更高版本中。

The problem lies a little in "25 unique random". 问题在于“25个独特随机”。 Displaying 25 random numbers is as easy as 显示25个随机数就像

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) . 由于9,999,999,999超过Int32.MaxValue,我建议使用Math.Floor(r.NextDouble() * 10000000000 + 1000000000)而不是r.Next(1,100)

Since your numbers are that long, you should not need to worry about duplicates. 由于你的数字很长,你不必担心重复。 They are very very unlikely. 他们非常不可能。

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 . 有关使用12位的样本在这里做 Hope this helps. 希望这可以帮助。

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. 这将确保数字总是10个字符(数字)长。 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. 如果您希望能够有超过十位数,则可以在10和最大位数之间随机生成位数。 Then generate each digit (or group of digits) randomly in a StringBuilder or List . 然后在StringBuilderList中随机生成每个数字(或数字组)。 You can use the same HashSet method I used above to ensure uniqueness. 您可以使用我上面使用的相同HashSet方法来确保唯一性。

 Random rnd = new Random(table);
 for(int i = 0; i < 25; ++i) {
   Console.WriteLine("{0}", rnd.Next(50, 50+i) 
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM