如何在角色中生成5位数 [英] How to generate 5 Digit Number in a role

查看:67
本文介绍了如何在角色中生成5位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,我需要一些帮助,这就是我想要实现的目标。我希望生成5个随机数并将它们存储在数据库示例中

13456

12564

35565

34466

34566

23456

34677





Good day, Please i need some help, this is what i want to achieve. I want to generate like 5 random numbers and have them stored in the database example
13456
12564
35565
34466
34566
23456
34677


//I know that using this 
 static Random rand = new Random();
 lbl1.Text = Convert.ToString(rand.Next(10000, 99999));
gives me 23445
my concern now is how to generate those numbers like 30 at same time as i have explaned above and have them saved in the database...

thanks, I do appreciate your effort..

推荐答案

private const int NumberOfRandomInts = 30;

// initialize the Random generator outside any loop !
Random rnd = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);

List<int> rndIntList = new List<int>(NumberOfRandomInts );

while(rndIntList.Count < NumberOfRandomInts )
{
    int i = rnd.Next(10000, 99999);

    if (rndIntList.Contains(i)) continue;

    rndIntList.Add(i);
}</int></int>

现在根据需要将'rndIntList的内容存储在您的数据库中。

Now store the content of 'rndIntList in your Database as required.


查看下面的代码,它将为您提供30个随机的5位数字。



Check out below code, it will give you 30 random 5 digits numbers.

List<int> numbers = new List<int>();

for (; numbers.Count < 30; )
{
    int i = new Random().Next(10000, 99999);
    if (numbers.Contains(i)) continue;

    numbers.Add(i);
}





如果代码中提到了条件,那么这些数字不应包含重复的数字。您可以根据需要更改for循环条件。



if condition is been mentioned in the code, so that numbers should not contain duplicate numbers. You can change the for loop condition depend how many numbers you want.


这篇关于如何在角色中生成5位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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