Generating random, unique values C#

前端 未结 17 1176
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 14:26

I\'ve searched for a while and been struggling to find this, I\'m trying to generate several random, unique numbers is C#. I\'m using System.Random, and I\'m us

17条回答
  •  遇见更好的自我
    2020-11-22 15:08

    unique random number from 0 to 9

          int sum = 0;
            int[] hue = new int[10];
            for (int i = 0; i < 10; i++)
            {
    
                int m;
                do
                {
                    m = rand.Next(0, 10);
                } while (hue.Contains(m) && sum != 45);
                if (!hue.Contains(m))
                {
                    hue[i] = m;
                    sum = sum + m;
                }
    
            }
    

提交回复
热议问题