generate random numbers with no repeat in c#

前端 未结 7 1121
萌比男神i
萌比男神i 2020-12-12 07:37

How can I generate random numbers with no repeat in C#. I have one array and I want to fill every room with random numbers from 0 to 9. Each room shoud have diffrent numbers

相关标签:
7条回答
  • 2020-12-12 08:00

    100 % right

       public int[] UniqeRandomArray(int size , int Min , int Max ) {
    
            int [] UniqueArray = new int[size];
            Random rnd = new Random();
            int Random;
    
            for (int i = 0 ; i < size ; i++) {
    
                Random = rnd.Next(Min, Max);
    
                for (int j = i; j >= 0 ; j--) {
    
                    if (UniqueArray[j] == Random)
                    { Random = rnd.Next(Min, Max); j = i; }
    
                }
    
                UniqueArray[i] = Random;
    
            }
    
            return UniqueArray;
    
        }
    

    // Notice to be unique [Max - Min > size] NOT equal

    0 讨论(0)
  • 2020-12-12 08:01

    This will create a unique range of 1 to rangeEx inclusive. The next two lines create a random number Generator and orders the IEnumerable range with a randome number. this is then called with ToArray and returned!

       private int[] RandomNumber(int rangeEx)
        {
            var orderedList = Enumerable.Range(1, range);
            var rng = new Random();
            return orderedList.OrderBy(c => rng.Next()).ToArray();
        }
    
    0 讨论(0)
  • 2020-12-12 08:03

    Your problem is that you are creating the Random object in every loop. The Random object must be created only once. Try this instead:

    Random rnd = new Random(); // <-- This line goes out of the loop        
    for (int i = 0; i < 20; i++) {
        int temp = 0;
        temp = rnd.Next(0, 9);
        page[i] = temp;
    }
    
    0 讨论(0)
  • 2020-12-12 08:04
            ArrayList page=new ArrayList();
            int random_index;            
            random rnd = new Random();
    
            for (int i = 0; i < 20; i++)
            {
                do                            
                {
                    random_index = rnd.Next(10);
                    if (!(page.Contains(random_index)))
                                    break;
                } while (page.Contains(random_index));
                page.Add(random_index);
            }
    
    0 讨论(0)
  • 2020-12-12 08:04
        public Form1()
        {
            InitializeComponent();
        }
        int A, B;
        string Output;
        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 20; i++)
            {
                while (A == B)
                {
                    Random r = new Random();
                    A = r.Next(1, 6);
                }
                Output = Output + A;
                B = A;
            }
            textBox1.Text = Output;
        }
    

    Output: 24354132435213245415 (not repeating)

    0 讨论(0)
  • 2020-12-12 08:14

    With such a small list of numbers to choose from you can simply generate a list that contains all of them and then shuffle them.

    0 讨论(0)
提交回复
热议问题