| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1575 人关注过本帖
标题:C#关于随机数抽取问题
取消只看楼主 加入收藏
wwfhz123
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-9-29
结帖率:0
收藏
已结贴  问题点数:20 回复次数:8 
C#关于随机数抽取问题
1-33数中 随即抽取6个数的和是97  
  
用C#做桌面应用程序

每个数不能在一组里重复出现2次

如果再在一个TextBox里自己设置一个总和 然后在抽取  

还有 是1-33中  不能有0  

改如何实现

用C#做

还有 可不可以 一次性抽取多个6个数的数组

[ 本帖最后由 wwfhz123 于 2010-9-29 21:01 编辑 ]
搜索更多相关主题的帖子: 随机数 抽取 
2010-09-29 20:34
wwfhz123
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-9-29
收藏
得分:0 
有没有人帮助一下

急需
2010-09-29 21:41
wwfhz123
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-9-29
收藏
得分:0 
能不能提供下源码呢。。
2010-09-29 22:24
wwfhz123
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-9-29
收藏
得分:0 
谢谢 我搞定了

我用我的方法做出来 能抽取随机数 但是有重复的

还有最后一个疑问 可以不可一次多抽取几组这样数组

最还还能排序一下 从小到大

[ 本帖最后由 wwfhz123 于 2010-9-29 23:34 编辑 ]
2010-09-29 23:31
wwfhz123
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-9-29
收藏
得分:0 
刚刚测试了一下

还是有重复的随机数 为什么阿
2010-09-29 23:55
wwfhz123
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-9-29
收藏
得分:0 
        private void btnExtraction_Click(object sender, EventArgs e)
        {
            if (input())//验证为否为空
            {
                string Num;

                Num = txtNum.Text;

                int sum = 0, total = int.Parse(Num);

                int numbert = 6;
                int i = 1;
                int temp = 0;
                int startnumber = 1, endnumber = 33;
                int[] number = new int[34];
                int[] random = new int[6];
                for (i = 1; i <= endnumber; i++)
                {
                    number[i] = i;
                }
                Random R = new Random();
                temp = R.Next(startnumber, endnumber);
                i = 1;
                random[0] = temp;
                do
                {
                    for (i = 1; i < numbert; i++)
                    {
                        random[i] = 0;
                        temp = R.Next(startnumber, endnumber);
                        for (int j = 1; j <= i; j++)
                        {
                            if (temp != random[j])
                            {
                                random[i] = temp;
                            }
                            else
                            {
                                temp = R.Next(startnumber, endnumber);
                            }
                        }
                    }
                    sum = 0;
                    for (i = 0; i < numbert; i++)
                    {
                        sum += random[i];
                    }
                } while (sum != total);

                string test = "";

                for (i = 0; i < numbert; i++)
                {
                    test += random[i].ToString() + "    ";
                }
                txtNum1.Text = random[0].ToString();
                txtNum2.Text = random[1].ToString();
                txtNum3.Text = random[2].ToString();
                txtNum4.Text = random[3].ToString();
                txtNum5.Text = random[4].ToString();
                txtNum6.Text = random[5].ToString();
            }

还是有重复的 为什么阿

[ 本帖最后由 wwfhz123 于 2010-9-30 00:01 编辑 ]
2010-09-30 00:00
wwfhz123
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-9-29
收藏
得分:0 
如果想抽取多组呢。。
2010-09-30 10:53
wwfhz123
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-9-29
收藏
得分:0 
            if (input())//验证是否为空
            {
                string Num;

                Num = txtNum.Text;

                int sum = 0, total = int.Parse(Num);

                int numbert = 6;
                int i = 1;
                int temp = 0;
                int startnumber = 1, endnumber = 33;
                int[] number = new int[34];
                int[] random = new int[6];
                for (i = 1; i <= endnumber; i++)
                {
                    number[i] = i;
                }
                Random R = new Random();
                temp = R.Next(startnumber, endnumber);
                i = 1;
                random[0] = temp;
                do
                {
                    for (i = 1; i < numbert; i++)
                    {
                        random[i] = 0;
                        int j = 0;
                        bool flag = false;
                        temp = R.Next(startnumber, endnumber);
                        do
                        {
                            for (j = 0; j < i; j++)
                            {
                                if (temp != random[j])
                                {
                                    flag = true;
                                }
                                else
                                {
                                    j = -1;
                                    temp = R.Next(startnumber, endnumber);
                                    flag = false;
                                }
                            }

                        } while (flag == false);
                        random[i] = temp;
                    }
                    sum = 0;
                    for (i = 0; i < numbert; i++)
                    {
                        sum += random[i];
                    }
                } while (sum != total);

                string test = "";

                for (i = 0; i < numbert; i++)
                {
                    test += random[i].ToString() + " ";
                }
                txtNum1.Text = random[0].ToString();
                txtNum2.Text = random[1].ToString();
                txtNum3.Text = random[2].ToString();
                txtNum4.Text = random[3].ToString();
                txtNum5.Text = random[4].ToString();
                txtNum6.Text = random[5].ToString();
            }

这串代码 应该把while放在何处阿...
2010-09-30 14:08
wwfhz123
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-9-29
收藏
得分:0 
我把你的代码 放到C#里会报错

好多语法 我都没见过

            var tempList = from t1 in intList
                           orderby Guid.NewGuid()
                           select t1;
这个就不知道什么意思
2010-09-30 16:37
快速回复:C#关于随机数抽取问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017387 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved