| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1575 人关注过本帖
标题:C#关于随机数抽取问题
只看楼主 加入收藏
wwfhz123
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-9-29
结帖率:0
收藏
已结贴  问题点数:20 回复次数:15 
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
zhaoyang1008
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:30
专家分:110
注 册:2010-9-15
收藏
得分:6 
先用random在1-33中间随机抽取5个数,然后用总和减去五个数如果得出的数是负数则继续前面的循环,直到减出的第六个数为正数
2010-09-29 21:54
wwfhz123
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-9-29
收藏
得分:0 
能不能提供下源码呢。。
2010-09-29 22:24
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
收藏
得分:6 
给你一些代码:最后的textBox1.Text 就显示这几个数是什么!!
int sum = 0, total = 97;
            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() + " ";
            }
            textBox1.Text = test;
收到的鲜花
  • wwfhz1232010-09-29 23:32 送鲜花  3朵   附言:好文章 谢谢
2010-09-29 22:25
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
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
收藏
得分:0 
非常抱歉,逻辑计算有点失误,已经改正过来了:
int sum = 0, total = 97;
            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() + " ";
            }
            textBox1.Text = test;
2010-09-30 09:10
wwfhz123
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-9-29
收藏
得分:0 
如果想抽取多组呢。。
2010-09-30 10:53
快速回复:C#关于随机数抽取问题
数据加载中...
 
   



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

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