如何在不重复的情况下自动生成介于10000到99999之间的数字 [英] How to auto generate number between 10000 to 99999 without repeat number

查看:1147
本文介绍了如何在不重复的情况下自动生成介于10000到99999之间的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在没有重复数字的情况下自动生成介于10000到99999之间的数字

解决方案

使用 Random class。

然后将数字放入字典中。

生成下一个数字。

如果数字存在于字典创建一个新的数字,否则显示这个数字。


从你的问题不清楚你想要的地方代码隐藏或数据库查询。



如果想在代码隐藏中试试这个: -

随机数=  new 随机(); 

int genNumber = number.nextInt();

while true
{
genNumber = number.nextInt();
if (genNumber > 10000 && genNumber < 99999
{
断裂;
}
}
系统。 out .println( < span class =code-string>随机数: + genNumber);







sql查询: -



  SELECT  FLOOR(RAND() *  99999  AS  random_num 
FROM table_Name
WHERE col_Name NOT IN SELECT my_number FROM numbers_mst)
LIMIT 1


为什么不使用标准的 Random.Next方法(Int32,Int32) [ ^ ]?

随机rand =  new  Random(); 
int value = rand.Next( 10000 100000 );



如果您在循环中生成随机数,你需要在循环外创建Random的实例(类静态readonly字段对此有好处)。



在SQL Server中你可以使用这段代码获取单个随机数

  SELECT   10000  + CAST(RAND()*( 100000   -   10000 )< span class =code-keyword> AS   INT 



如果您需要为结果集的每一行生成随机数,您必须使用 NEWID [ ^ ]而不是 RAND [ ^ ]功能:

  SELECT   10000  +(ABS(CAST(CAST) (NEWID() AS   VARBINARY  AS   INT ))%( 100000   -   10000  ))


How to auto generate number between 10000 to 99999 without repeat number

解决方案

Create random numbers between the give range using the Random class.
Then put the number in a dictionary.
Generate the next number.
If the number is present in the dictionary create a new number else display this number.


From your question its not clear that where you want this means in code behind or database query.

if want in code-behind try this:-

Random number = new Random();

int genNumber = number.nextInt();

while (true)
 {
    genNumber = number.nextInt();
    if (genNumber > 10000 && genNumber < 99999)
                    {
        break;
      }
}
System.out.println("Random  Number:"+genNumber);




sql query:-

SELECT FLOOR(RAND() * 99999) AS random_num
FROM table_Name
WHERE "col_Name" NOT IN (SELECT my_number FROM numbers_mst)
LIMIT 1


Why just not use standard Random.Next Method (Int32, Int32)[^]?

Random rand = new Random();
int value = rand.Next(10000, 100000);


In case you are generating random numbers in loop, you need to create instance of Random outside the loop (class static readonly field would be good for this).

In SQL Server you can use this code for getting single random number:

SELECT 10000 + CAST(RAND() * (100000 - 10000) AS INT)


In case you need to generate random number for each row of your result set, you have to use NEWID[^] instead of RAND[^] function:

SELECT 10000 + (ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % (100000 - 10000))


这篇关于如何在不重复的情况下自动生成介于10000到99999之间的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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