Generating random numbers without repeating.C#

前端 未结 11 1861
臣服心动
臣服心动 2020-12-01 17:53

Hi everyone I am trying to generate 6 different numbers on the same line in c# but the problem that i face is some of the numbers are repeating on the same line.Here is my c

11条回答
  •  时光取名叫无心
    2020-12-01 18:11

       public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            int[] que = new int[6];
            int x, y, z;
            Random ran = new Random();            
            for ( x = 0; x < 6; x++)
            {
                que[x] = ran.Next(1,49);
                for (y = x; y >= 0; y--)
                {
                    if (x == y)
                    {
                        continue;
                    }
                    if (que[x] == que[y])
                    {
                        que[x] = ran.Next(1,49);
                        y = x;
                    }
    
    
                }
            }
            listBox1.Items.Clear();
            for (z = 0; z < 6**strong text**; z++)
            {
                listBox1.Items.Add(que[z].ToString());
            }
        }
    }
    

提交回复
热议问题