来自int数组的C#随机数 [英] C# random numbers from int array

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

问题描述

美好的一天。

让我先说我错过了一些明显的东西,我知道,但似乎无法得到它。

我正在进行随机化应用程序。

我需要从我的数据库中提取活动用户列表(使用USERID),将它们加载到 Datagridview 中,这样可以正常工作,之后我将USERID加载到 int [] 中,也可以正常工作。现在我的问题是我需要将这些USERID循环到一个随机函数中并用它填充一个不同的 Datagridview ,并且随机只应选择数组中的USERID。 />
这是我已经走了多远。





Good day all.
Let me start by saying I am missing something obvious and I know, but cant seem to get it.
I am working on a randomization application.
I need to pull a list of active users(with USERID's) from my database, load them into a Datagridview, Well that works fine, now after that I load the USERID's into a int[], also works fine. Now my problem is that I need to loop these USERID's into a random function and populate a different Datagridview with it and the random is only supposed to select USERID's in the array.
This is how far I have gotten.


conn.Open();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < select.Length; i++)
            {
                ds = new DataSet();
                int use = Users.Next(15, Int32.Parse(select.GetValue(i).ToString()));
                if (select.Contains(use))
                {
                    sb.Append(use + "\r\n");
                    da = new SqlDataAdapter("SELECT USERID,FULLNAME FROM USERS WHERE TYPE = 'EMPLOYEE' AND ACTIVE = 'True' AND USERID ='" + use + "' ORDER BY USERID", conn);

                }                
            }
            
            build = new SqlCommandBuilder(da);
            da.Fill(ds, "USERS");
            dgSelected.DataSource = ds;
            dgSelected.DataMember = "USERS";
            conn.Close();





select是 int []



请帮忙吗?



select is the int[]

Any help please?

推荐答案

我这样做的方法是不使用数组但改为列表:

The way I do it is not to use an array but a List instead:
List<int> newOrder = new List<int>();
while (users.Count > 0)
    {
    int removeIndex = rand.Next(users.Count)
    newOrder.Add(users[removeIndex]);
    users.RemoveAt(removeIndex);
    }

这样做是随机选择一个项目,然后将其删除,以便不能重复。

What this does is select an item at random, and then remove it so it can't be repeated.


这篇关于来自int数组的C#随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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