Generating random, unique values C#

前端 未结 17 1153
佛祖请我去吃肉
佛祖请我去吃肉 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 14:56

    You might try shuffling an array of possible ints if your range is only 0 through 9. This adds the benefit of avoiding any conflicts in the number generation.

    var nums = Enumerable.Range(0, 10).ToArray();
    var rnd = new Random();
    
    // Shuffle the array
    for (int i = 0;i < nums.Length;++i)
    {
        int randomIndex = rnd.Next(nums.Length);
        int temp = nums[randomIndex];
        nums[randomIndex] = nums[i];
        nums[i] = temp;
    }
    
    // Now your array is randomized and you can simply print them in order
    for (int i = 0;i < nums.Length;++i)
        Console.WriteLine(nums[i]);
    
    0 讨论(0)
  • 2020-11-22 14:59

    hi here i posted one video ,and it explains how to generate unique random number

      public List<int> random_generator(){
    
      Random random = new Random();
    
       List<int> random_container = new List<int>;
    
         do{
    
           int random_number = random.next(10);
    
          if(!random_container.contains(random_number){
    
           random_container.add(random_number)
      }
    }
       while(random_container.count!=10);
    
    
         return random_container; 
      }
    

    here ,,, in random container you will get non repeated 10 numbers starts from 0 to 9(10 numbers) as random.. thank you........

    0 讨论(0)
  • 2020-11-22 15:05

    NOTE, I dont recommend this :). Here's a "oneliner" as well:

    //This code generates numbers between 1 - 100 and then takes 10 of them.
    var result = Enumerable.Range(1,101).OrderBy(g => Guid.NewGuid()).Take(10).ToArray();
    
    0 讨论(0)
  • 2020-11-22 15:08

    It's may be a little bit late, but here is more suitable code, for example when you need to use loops:

                List<int> genered = new List<int>();
    
                Random rnd = new Random();
    
                for(int x = 0; x < files.Length; x++)
                {
                    int value = rnd.Next(0, files.Length - 1);
                    while (genered.Contains(value))
                    {
                        value = rnd.Next(0, files.Length - 1);
                    }
                    genered.Add(value);
    
                    returnFiles[x] = files[value];
                }
    
    0 讨论(0)
  • 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;
                }
    
            }
    
    0 讨论(0)
  • 2020-11-22 15:09

    Same as @Habib's answer, but as a function:

    List<int> randomList = new List<int>();
    int UniqueRandomInt(int min, int max)
    {
        var rand = new Random();
        int myNumber;
        do
        {
           myNumber = rand.Next(min, max);
        } while (randomList.Contains(myNumber));
        return myNumber;
    }
    

    If randomList is a class property, UniqueRandomInt will return unique integers in the context of the same instance of that class. If you want it to be unique globally, you will need to make randomList static.

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