当前位置:编程学习 > C#/ASP.NET >>

帮忙修改一下

 private void button1_Click(object sender, EventArgs e)
        {
            int i;
            string s = "";
            Random ro = new Random();
            for (i = 0; i < 8; i++)
            {
                iResult = ro.Next(1, 10);
                r[i] = iResult;
                s = s + iResult.ToString() + " ";
            }
            textBox1.Text = s;

        }
以上这段代码可以产生8个随机数,但我想请教一下如何产生8个不同的数呢?麻烦大家帮我把这段代码给修改一下了,谢谢!! --------------------编程问答-------------------- 生成的的时候判断一下,和之前有一样的,就再生成一个新的

不过你的随机空间就0-9,共10个数

你要出8个,越到后面越重复的可能性越大 --------------------编程问答-------------------- Random ro = new Random(100); --------------------编程问答-------------------- <p>写的太牛<font color=#ff1493 size=5 face=黑体><strong><em><u><strike>逼了,狂</strike></u></em></strong></font>顶啊</p>   --------------------编程问答-------------------- eret --------------------编程问答-------------------- sdfsdg --------------------编程问答--------------------         static void Main(string[] args)
        {
            ArrayList AList = new ArrayList();//定义可变长数组
            Random rd = new Random();
            int num;//将每次产生的随机数赋给此变量
            bool _bool = true;//判断是否有重复,初始时,数组中没有元素,不需要判断,固赋初值为True
            //产生8个不同的随机数
            while (AList.Count < 8)
            {
                num = rd.Next(0, 9);
                for (int i = 0; i < AList.Count; i++)
                {
                    //如果重复,跳出此循环,重新产生随机数
                    if (num == Convert.ToInt32(AList[i]))
                    {
                        _bool = false;
                        break;
                    }

                    //如果与数组中该项不同,则为True
                    else
                    {
                        _bool = true;
                    }
                }
                //上面循环后,如果_bool仍为True,则说明此次产生的随机数在数组中无重复,则将此随机数赋给数组
                if (_bool == true)
                {
                    AList.Add(num.ToString());
                }
            }

            for (int i = 0; i < AList.Count; i++)
            {
                Console.WriteLine(AList[i]);
            }
            Console.ReadLine();
        } --------------------编程问答-------------------- 10个数里面生成8个吗?
--------------------编程问答--------------------
ArrayList arraylist=new ArrayList();
Random rand=new Random();
int array1=rand.next(1,10)
arraylist.Add(array1);
while(arraylist.Count<9)
{
int tamp=rand.next(1,10);
if(arraylist.Contains(temp))
arraylist.Add(temp);
}
然后在把arraylist的值拷贝到你的目标数组里头就可以了 --------------------编程问答-------------------- if(!arraylist.Contains(temp)) --------------------编程问答-------------------- Random ro = new Random(time); --------------------编程问答-------------------- 随机数产生的范围可以是1-100,关键是如何产生8个不一样的随机数
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,