Generating random numbers without repeating.C#

前端 未结 11 1804
臣服心动
臣服心动 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:06

    If you are not worried about the min, max, and range then you can use this.

     var nexnumber = Guid.NewGuid().GetHashCode();
            if (nexnumber < 0)
            {
                nexnumber *= -1;
            }
    
    0 讨论(0)
  • 2020-12-01 18:09

    Make it a while loop and add the integers to a hashset. Stop the loop when you have six integers.

    0 讨论(0)
  • 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());
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 18:16

    Paste the below in the class as a new method

        public int randomNumber()
        {
            var random = new Random();
            int randomNumber = random.Next(10000, 99999);
            return randomNumber;
        }
    

    And use the below anywhere in the tests wherever required

    var RandNum = randomNumber();
    
    driver.FindElement(By.CssSelector("[class='test']")).SendKeys(**RandNum**);
    
    0 讨论(0)
  • 2020-12-01 18:17
    listNumbers.AddRange(Enumerable.Range(1, 48)
                                   .OrderBy(i => rand.Next())
                                   .Take(6))
    
    0 讨论(0)
提交回复
热议问题