需要生成非重复的随机数 [英] Need to generate non repetitive random numbers

查看:112
本文介绍了需要生成非重复的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要使用随机数类生成非重复数字。

我的要求是我的DB中有50个问题。我的测验只包含20个问题。所以我需要挑选50个问题。



任何人都可以帮助我吗?如果可能的话请提供一个例子。

Hi,

I need to generate non repetitive numbers using random number class.
My requirement is I have 50 questions in my DB. My quiz contains only 20 questions. So I need to pick 20 questions out of 50.

Can any one help me ? If possible please provide an example.

推荐答案

检查此下面的查询以获取帮助



Check this bellow query for help

SELECT top 20 table_name, NewId() magic_number
FROM information_schema.tables


看看这里:

C#解决方案:

http://www.functionx .com / csharp1 / topics / randomnumbers.htm [ ^ ]

随机生成循环中的数字不重复。 [ ^ ]



T-SQL解决方案:

http://www.sqlservercentral.com/Forums/Topic967923-371-1.aspx [ ^ ]

http://stackoverflow.com/questions/2247554/random-number-on-sql-without-using-newid [ ^ ]

http://forums.asp.net/t/1791906。 aspx / 1 [ ^ ]
Have a look here:
C# solutions:
http://www.functionx.com/csharp1/topics/randomnumbers.htm[^]
Generate random numbers in loop without repeating.[^]

T-SQL solutions:
http://www.sqlservercentral.com/Forums/Topic967923-371-1.aspx[^]
http://stackoverflow.com/questions/2247554/random-number-on-sql-without-using-newid[^]
http://forums.asp.net/t/1791906.aspx/1[^]


您好,



您可以使用类似于下面所示的代码来执行此操作。虽然要生成适合创建随机密码的加密安全随机数,例如,使用从 System.Security.Cryptography.RandomNumberGenerator [ ^ ]。

Hello,

You can use code similar to one shown below to do this. Although to generate a cryptographically secure random number suitable for creating a random password, for example, use a class derived from System.Security.Cryptography.RandomNumberGenerator[^].
Random rand = new Random();
ArrayList result = new ArrayList();

// Generate and display 20 random integers between 0 and 50.//
Console.WriteLine("20 Random integers between 0 and 50:");
while (result.Count < 20) {
    int nextNo = rand.Next(51);
    if (result.Contains(nextNo)) continue;
    result.Add(nextNo);
    Console.WriteLine("{0,8:N0}", nextNo);
}
Console.WriteLine();
Console.ReadKey();



问候,


Regards,


这篇关于需要生成非重复的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆